Browse files

Update javascript notes

Transferred from DSA
  • Loading branch information...
hparra committed Dec 12, 2016
1 parent 60796dd commit 0a71cf83137de4f5e0e52040c2cfe81a62aa4ece
Showing with 56 additions and 2 deletions.
  1. +56 −2
@@ -1,7 +1,61 @@

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


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.


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

- implemented using _shape trees_
- can be used as a hash table
- but its better to use an array for this



- [How JavaScript Objects are Implemented](
- [The pitfalls of using objects as maps in JavaScript](
[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.

0 comments on commit 0a71cf8

Please sign in to comment.