Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are JavaScript arrays actually linked lists?

I'm new to Javascript, and notice that you don't need to specify an array's size and often see people dynamically creating arrays one element at time. This would be a huge performance problem in other languages as you would constantly need to reallocate memory for the array as it increases in size.

Is this not a problem in JavaScript? If so, then is there a list structure available?

like image 250
nw. Avatar asked Sep 01 '25 23:09

nw.


2 Answers

Javascript arrays are typically implemented as hashmaps (just like Javascript objects) with one added feature: there is an attribute length, which is one higher than the highest positive integer that has been used as a key. Nothing stops you from also using strings, floating-point numbers, even negative numbers as keys. Nothing except good sense.

like image 94
Michael Lorton Avatar answered Sep 03 '25 11:09

Michael Lorton


It most likely depends on what JavaScript engine you use.

Internet Explorer uses a mix of sparse arrays and dense arrays to make that work. Some of the more gory details are explained here: http://blogs.msdn.com/b/jscript/archive/2008/04/08/performance-optimization-of-arrays-part-ii.aspx.

like image 23
vcsjones Avatar answered Sep 03 '25 11:09

vcsjones