Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How best to do a JavaScript array with non-consecutive indexes?

Tags:

I'm writing a Google Chrome extension, in JavaScript, and I want to use an array to store a bunch of objects, but I want the indexes to be specific non-consecutive ID numbers.

(This is because I need to be able to efficiently look up the values later, using an ID number that comes from another source outside my control.)

For example:

var myObjects = [] ;  myObjects[471] = {foo: "bar"} ;  myObjects[3119] = {hello: "goodbye"} 

When I do console.log(myObjects), in the console I see the entire array printed out, with all the thousands of 'missing' indexes showing undefined.

My question is: does this matter? Is this wasting any memory?

And even if it's not wasting memory, surely whenever I loop over the array, it wastes CPU if I have to manually skip over every missing value?

I tried using an object instead of an array, but it seems you can't use numbers as object keys. I'm hoping there's a better way to achieve this?

like image 776
callum Avatar asked Jan 22 '11 22:01

callum


People also ask

Can JavaScript arrays have mixed types?

As mentioned earlier, JavaScript supports mixed arrays. That means you can create an array with numbers and strings or other objects.

Does order of array in JavaScript matter?

Answer and Explanation First, the array sort method sorts your original array and also returns a reference to that array. This means that when you write arr2. sort() , the arr2 array object is sorted. It turns out, however, the sort order of the array doesn't matter when you're comparing objects.

What is the correct way to JavaScript array?

Using an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.

Are arrays in JavaScript zero indexed?

JavaScript arrays are zero-indexed: the first element of an array is at index 0 , the second is at index 1 , and so on — and the last element is at the value of the array's length property minus 1 .


1 Answers

First of all, everyone, please learn that what the for-in statement does is called enumeration (though it's an IterationStatement) in order to differentiate from iteration. This is very important, because it leads to confusion especially among beginners.

To answer the OP's question: It doesn't take up more space (test) (you could say it's implementation dependent, but we're talking about a Google Chrome Extension!), and it isn't slower either (test).

Yet my advice is: Use what's appropriate! In this situation: use objects!

What you want to do with them is clearly a hashing mechanism, keys are converted to strings anyway so you can safely use object for this task.

I won't show you a lot of code, other answers do it already, I've just wanted to make things clear.

// keys are converted to strings  // (numbers can be used safely) var obj = {} obj[1] = "value" alert(obj[1])   // "value" alert(obj["1"]) // "value" 

Note on sparse arrays

The main reason why a sparse array will NOT waste any space is because the specification doesn't say so. There is no point where it would require property accessors to check if the internal [[Class]] property is an "Array", and then create every element from 0 < i < len to be the value undefined etc. They just happen to be undefined when the toString method is iterating over the array. It basically means they are not there.

11.2.1 Property Accessors

The production MemberExpression : MemberExpression [ Expression ] is evaluated as follows:

  1. Let baseReference be the result of evaluating MemberExpression.
  2. Let baseValue be GetValue(baseReference).
  3. Let propertyNameReference be the result of evaluating Expression.
  4. Let propertyNameValue be GetValue(propertyNameReference).
  5. Call CheckObjectCoercible(baseValue).
  6. Let propertyNameString be ToString(propertyNameValue).
  7. If the syntactic production that is being evaluated is contained in strict mode code, let strict be true, else let strict be false.
  8. Return a value of type Reference whose base value is baseValue and whose referenced name is propertyNameString, and whose strict mode flag is strict.

The production CallExpression : CallExpression [ Expression ] is evaluated in exactly the same manner, except that the contained CallExpression is evaluated in step 1.

ECMA-262 5th Edition (http://www.ecma-international.org/publications/standards/Ecma-262.htm)

like image 109
25 revs, 4 users 83% Avatar answered Oct 27 '22 08:10

25 revs, 4 users 83%