Answer the question
In order to leave comments, you need to log in
Which is faster array or object?
I write a chat on nodejs. I store data as an object.
const companies = {
2: {
id: 2,
company_name: 'My company',
users: {
1: {
id: 1,
name: 'John',
messages: [...]
}
}
}
}
<span>{{ companies[company_id].users[user_id].name }}</span>
const objResult = obj[key];
//VS
const arrResult = arr.find(el => el.id === key);
Answer the question
In order to leave comments, you need to log in
It reminded me of a story when a speaker compared a search in Yandex and a direct indication of a site in the address bar. Like, when he writes the address of the site, he immediately goes to it, and supposedly the search does not turn on. However, even if you specify the site address directly, the search still turns on: you entered the site address, but this is not the exact path to the server where the data is stored. While there is a request for data, and while they go back, several searches are included at different stages.
Same here. In fact, the system needs to get some value from memory. Arrays are initially sharpened for the machine organization of memory: in order. It is logical to assume that if you need an Ne value, then it will get out of memory faster, since it is Ne in the memory itself, starting from a certain cell (roughly speaking). More complex associative arrays (or objects) are more complexly organized in memory (sorry for the tautology). An associative array is at least two regular arrays. Accordingly, when performing a search on it, at least two searches are already working.
it's always iterating over the entire array each time.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question