0

Recently i had to make an Array with values at large indexes (due to plugin constraints). eg:

var names[100000] = "a";
var names[150000] = "b" ... and so on till 5 large indexes.

and in between all values are undefined names[100001] //undefined.

Now my doubt is Since the array has only 5 elements but if i do

names.length //it is 300001 

its a large Array. I am not iterating this array nor i am running any loop through it. I will get the values directly through their defined indexes from the array.

So will this structure make any significant performance differences on the Browser or is it alright to use this as long as the number of values in the array is less irrespective of its indexes and no iteration is involved.

Thanks

4
  • 3
    If you only have static keys and you only need to access the values directly using those keys - then use an object instead of an array. Commented May 17, 2017 at 11:27
  • @CBroe i can't as i said "(due to plugin constraints)". that was my first solution though. Commented May 17, 2017 at 11:28
  • What are the plugin constraints? A parameter of a function or a return value? Commented May 17, 2017 at 11:30
  • its a plugin where in need to pass custom label on slider for the Pips at specific intervals. Commented May 17, 2017 at 11:31

2 Answers 2

2

The only thing that differentiates an array from a plain object is its length property and how it behaves (and a few array specific methods of course). The length value simply increases with certain operations, like setting a numeric property or pushing a new element. That's it in a nutshell. The array doesn't actually contain 100000 elements when you set the property 100000 to a value, all that's happening is that you're setting one property and the value of length is adjusted accordingly.

So, no, it won't have a lot of impact on performance, unless somebody actually iterates through the array using for (let i = 0; i < arr.length; i++).

Sign up to request clarification or add additional context in comments.

2 Comments

Basically what I was gonna say, but worded so much better :D
@Cerbrus That's what I thought when I saw your answer!
-1

You can create an array with the length, given by your plugin and work locally with an object to limit the iterations. After all your processing has been applied, you copy the values to the array and send it to the plugin's function.

Keep an array with the desired length as a buffer

var buffer = new Array(20000);

Internally work with an object

var data = {};

Assign values to the object

data[10001] = "foo";

Once transformations or data processing has been applied to the object, copy data to the buffer

for (key in data){
    buffer[key] = data[key];
}

Send buffer to the plugin. And clear data, if desired.

By doing so, you will not iterate more, than the actual data you processed.

3 Comments

That seems quite convoluted. I mean, if performance is a concern, just using a plain array is as fast as it gets.
@Jankapunkt i am thinking a way around just like your setup
Depends on how often you have to transform and iterate data. The principle is copied by how graphics engines send data to the gpu. Plus you need only to iterate the length of your internal object.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.