Skip to content

Instantly share code, notes, and snippets.

@tennisonchan
Last active June 17, 2018 07:58
Show Gist options
  • Save tennisonchan/38303d2649f60616f156d391ddbd01e5 to your computer and use it in GitHub Desktop.
Save tennisonchan/38303d2649f60616f156d391ddbd01e5 to your computer and use it in GitHub Desktop.

JavaScript engine fundamentals: Shapes and Inline Caches

link: https://mathiasbynens.be/notes/shapes-ics

JavaScript Engines:

  • V8; NodeJS
  • SpiderMoney; SpiderNode
  • Charkra; Node Charkra
  • JSC: powering Safari & React Native

JS source code -> parser -> Abstract Sytax Tree

Interpreter -> bytecode | (when func gets hot) | (deoptimizing) optimizing compiler -> optimized code

All js object are dictionaries with string keys, and the string keys map to a value called Property attributes. [MDN-Object.defineProperty] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty)

Property attributes can store:

  • Writeable: the properity can be reassigned to.
  • Enumberable: the properity can show up in fo-in loop.
  • Configurable: it is deletable properity.
obj = Object.defineProperties({}, {
  a: {
    value: 1,
    writable: false,
    enumerable: true,
    configurable: false
  },
});

obj.a = 11;
console.log(obj.a); // 1

delete obj.a;
console.log(obj.a); // 1

for(let x in obj) {
  console.log(x); // a
}
  • The upper limit size of the array is 2^32 - 1
new Array(2**32)
// Uncaught RangeError: Invalid array length

Sharp

  • Always initialize objects in the same way, so that they don't end up with different sharps

Inline Cache (IC)

The ingredient to make JS run fast.

  • Don't mess up with the property attributes of array elements, so that they can be stored and operated efficiently.
  • Avoid using Object.defineProperty in array index
  • It blows up the element backing store and turns the whole thing into an object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment