W
W
WebDev2020-04-15 12:59:26
JavaScript
WebDev, 2020-04-15 12:59:26

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: [...]
            }
        }
    }
}


More or less like this. I store the data in an object, not in an array, for two reasons:
1) It's more convenient to handle this way. For example, I can display the username directly in the markup (I use Vue).
<span>{{ companies[company_id].users[user_id].name }}</span>

2) This is faster, because we immediately access a specific property.

Then I decided to look at the difference. Wrote a small script that ran in the browser.
The essence of the script is that it looks for data a million times in the array and in the object. Simplified, it looks like this:
const objResult = obj[key];
//VS
const arrResult = arr.find(el => el.id === key);

To my dismay, searching through an array is orders of magnitude faster.
Probably the browser caches something and in general it is wrong to carry out such measurements in the browser. Then I went to the Internet and in the very first question on stackoverflow they write " Arrays are mostly faster than objects ".
I've looked in different sources, opinions vary.

Explain, please, why such a question is discussed at all and takes place? After all, accessing an object by key is not a search at all. The script immediately knows and immediately receives the data. In an array, on the contrary, it is always iterating over the entire array each time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Titov, 2020-04-15
@kirill-93

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.

If the keys are unique, which is the case in ordinary javascript arrays, then the search is unlikely to continue after finding the desired element.
In general, web applications are not exactly the area where you need to optimize queries, based on the architecture, I mean, when you get a gain of a couple of milliseconds. The page loads for a couple of seconds, the animations last 0.3s, and so on - against this background, the gain will be zero, even with 1 billion users
online project development. If the code is externally and logically similar to how the data is organized in everyday perception, this is convenient: you quickly understand and can be improved.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question