Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of array includes vs mapping to an Object and accessing it in JavaScript

Tags:

According to the fundamentals of CS the search functionality of an unsorted list has to occur in O(n) time where as direct access into an array will occur in O(1) time for HashMaps.

So is it more performant to map an array into a dictionary and then access the element directly or should I just use includes? This question is specifically for JavaScript because I believe this would come down to core implementation details of how includes() and {} is implemented.

let y = [1,2,3,4,5] y.includes(3) 

or...

let y = {           1: true,           2: true           3: true           4: true           5: true         } 5 in y 
like image 892
Luke Xu Avatar asked Nov 22 '18 04:11

Luke Xu


People also ask

What is the difference between map and array in JavaScript?

Map has a built-in ‘has’ function too. Note: Compared to the array’s includes function, Object’s hasOwnProperty and Set/Map’s has functions seem to perform close to O (1) in different tests, clearly more efficient in terms of larger data sets. 4. Removing Duplicates

What is the difference between array and object performance?

There are plenty of resources on the internet about array vs. object performance, but briefly: array manipulation is slower when you don’t know the index ( linear time, or O ( n )), because you have to iterate over each element until you find the one you’re looking to use.

Is there a performance test for JavaScript arrays and objects?

Here is a performance test of three possibilities: An excellent read about these topics at Smashing Magazine: Writing fast memory efficient JavaScript Show activity on this post. It's not really a performance question at all, since arrays and objects work very differently (or are supposed to, at least).

Are arrays easier to access than objects?

Accessing object properties is easy too: So far, arrays have been kind of a drag compared to objects. Doing anything with a single array element requires that you know the index, or requires a bit more code. Finally, with iteration, it’s time for the array to shine.


1 Answers

It's true that object lookup occurs in constant time - O(1) - so using object properties instead of an array is one option, but if you're just trying to check whether a value is included in a collection, it would be more appropriate to use a Set, which is a (generally unordered) collection of values, which can also be looked up in linear time. (Using a plain object instead would require you to have values in addition to your keys, which you don't care about - so, use a Set instead.)

const set = new Set(['foo', 'bar']); console.log(set.has('foo')); console.log(set.has('baz'));

This will be useful when you have to look up multiple values for the same Set. But, adding items to the Set (just like adding properties to an object) is O(N), so if you're just going to look up a single value, once, there's no benefit to this nor the object technique, and you may as well just use an array includes test.

like image 131
CertainPerformance Avatar answered Sep 22 '22 09:09

CertainPerformance