user730569
user730569

Reputation: 4004

Which javascript structure has a faster access time for this particular case?

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 []?

Upvotes: 0

Views: 97

Answers (4)

Eugene Naydenov
Eugene Naydenov

Reputation: 7295

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.

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340713

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.

Upvotes: 2

user797257
user797257

Reputation:

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.

Upvotes: 2

nekman
nekman

Reputation: 1919

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.

Upvotes: 0

Related Questions