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?
As mentioned earlier, JavaScript supports mixed arrays. That means you can create an array with numbers and strings or other objects.
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.
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.
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 .
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:
GetValue
(baseReference). GetValue
(propertyNameReference). CheckObjectCoercible
(baseValue). ToString
(propertyNameValue). 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With