HGPA

JavaScript

The most popular JavaScript interpreter is V8 used by Google Chrome and NodeJS. Others include SpiderMonkey (Firefox), JavaScriptCore aka Nitro (Safari) and Chakra (Microsoft Edge).

Array

Normal

A sequential array, e.g. [1,2,3], is implemented in a linear storage buffer (memory). Once array is full it increases in size. While convention is for it to double in size implementations differ.

Sparse

JS allows you to specify any i-th element in an array. For example:

const array = [];
array[0] = 1;
array[1] = 2;
array[3] = 3;

In this case array[2] is not defined. It is an array hole and this type of array is called a sparse array. In this case V8 will use a hash table to store the elements of the array. This eliminates the space wasted by empty spots.

HYPOTHESIS: Since a sparse array is implemented as an actual hash table, it may be faster to use an array instead of an object when you desire a hash table for your own application with numeric keys, e.g. sequential primary IDs. For example:

const map = []
map[532] = 'blah'
map[2727] = 'foo'

RESULT: No difference in performance. Why?

Object

(TODO)

REFERENCES

How JavaScript Objects are Implemented. Eddy Bruel. MLOC.JS. 2014-06-07. Explanation on implementing SpiderMonkey's Objects as shape trees. After 20 minutes there is discussion regarding arrays.

Breaking the JavaScript Speed Limit with V8. Daniel Clifford. Google I/O 2012. 2012-06-29.

The pitfalls of using objects as maps in JavaScript. Axel Rauschmayer. ②ality – JavaScript and more. 2012-01-03.

V8: an open source JavaScript engine. Lars Bak. Google. 2008-08-15.

Writing Fast, Memory-Efficient JavaScript Addy Osmani. Smashing Magazine. 2012-10-05.

Filename: javascript.md (Edit)
Modified: 2016-12-12 (0a71cf83137de4f5e0e52040c2cfe81a62aa4ece)
Created: 2016-08-09 (aafac627d061a929d625c2666c79aa56308ee020)