0

I need to map specific numbers to string values. These numbers are not necessarily consecutive, and so for example I may have something like this:

var obj = {};
obj[10] = "string1";
obj[126] = "string2";
obj[500] = "string3";

If I'm doing a search like this obj[126] would it be faster for me to use an object {} or an array []?

2
  • 1
    A quick google search returns several StackOverflow questions that are answered and should make it clear that each browser JS engine is implemented differently so each could have a different result: google.com/… Sep 26, 2012 at 20:31
  • 1
    Arrays are just objects, too - I don't think there is much difference or optimisation (especially with your sparse "array"). Test it.
    – Bergi
    Sep 26, 2012 at 20:31

4 Answers 4

2

There will be no difference. ECMAScript arrays, if sparse (that is don't have consecutive indices set) are implemented as hash tables. In any case, you are guaranteed the O(n) access time, so this shouldn't concern you at all.

1
2

I created a microbenchmark for you - check out more comprehensive test by @Bergi. On my browser object literal is a little bit slower, but not significantly. Try it yourself.

4
  • 1
    You are testing the assignment, which does require a tiny bit more overhead for an Array, but the question is about the search. "If I'm doing a search like this obj[126]..." Sep 26, 2012 at 20:35
  • Funny, in Chrome I get the object literal as faster, but yeah I'm more concerned about the search rather than assignment.
    – user730569
    Sep 26, 2012 at 20:38
  • 1
    Check out jsperf.com/sparse-objects, which uses some more and random values.
    – Bergi
    Sep 26, 2012 at 20:55
  • @Bergi: +1, I allowed myself to include your link in my answer, hope you don't mind (?) Sep 26, 2012 at 21:00
0

A JS-array is a object, so it should not matter what you choose.

Created a jsperf test (http://jsperf.com/array-is-object) to demonstrate this.

0

Definetely an object should be the best choice.

If you have such code:

var arr = [];
arr[10] = 'my value';

, your array becomes an array of 11 values

alert(arr.length); // will show you 11

, where first 10 are undefined.

Obviously you don't need an array of length 1000 to store just

var arr = [];
arr[999] = 'the string';

Also I have to notice that in programming you have to chose an appropriate classes for particular cases.
Your task is to make a map of key: value pairs and object is the better choice here.
If your task was to make an ordered collection, then sure you need an array.

UPDATE:

Answering to your question in comments.

Imagine that you have two "collections" - an array and an object. Each of them has only one key/index equal to 999.

If you need to find a value, you need to iterate through your collection.

For array you'll have 999 iterations.
For object - only one iteration.

http://jsfiddle.net/f0t0n/PPnKL/

var arrayCollection = [],
    objectCollection = {};

arrayCollection[999] = 1;
objectCollection[999] = 1;
var i = 0,
    l = arrayCollection.length;

for(; i < l; i++) {
    if(arrayCollection[i] == 1) {
        alert('Count of iterations for array: ' + i); // displays 999
    }
}

i  = 0;

for(var prop in objectCollection) {
    i++;
    if(objectCollection[prop] == 1) {
        alert('Count of iterations for object: ' + i); // displays 1
    }
}

Benchmark

In total:

You have to design an application properly and take into account possible future tasks which will require some different manipulations with your collection.
If you'll need your collection to be ordered, you have to chose an array.
Otherwise an object could be a better choice since the speed of access to its property is roughly same as a speed of access to array's item but the search of value in object will be faster than in sparse array.

4
  • 2
    Why would that make a difference? ...Regarding your update, JavaScript arrays can be sparse. Doing arr[10] = "val" gives you an Array with 1 member. At its core, it's really just an Object. Sep 26, 2012 at 20:32
  • 1
    length is misleading. It doesn't correspond to the actual size of the array in memory. See here: coredogs.com/article/… Sep 26, 2012 at 20:38
  • Only if you don't need to iterate through an array to find a value. See my update. Sep 26, 2012 at 20:47
  • I believe for...in is a more expensive operation, so there may be a threshold where iterating over empty items is still faster.
    – Shmiddty
    Sep 26, 2012 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.