Node.js: for each … in not working

Unfortunately node does not support for each … in, even though it is specified in JavaScript 1.6. Chrome uses the same JavaScript engine and is reported as having a similar shortcoming. You’ll have to settle for array.forEach(function(item) { /* etc etc */ }). EDIT: From Google’s official V8 website: V8 implements ECMAScript as specified in … Read more

Accessing line number in V8 JavaScript (Chrome & Node.js)

Object.defineProperty(global, ‘__stack’, { get: function(){ var orig = Error.prepareStackTrace; Error.prepareStackTrace = function(_, stack){ return stack; }; var err = new Error; Error.captureStackTrace(err, arguments.callee); var stack = err.stack; Error.prepareStackTrace = orig; return stack; } }); Object.defineProperty(global, ‘__line’, { get: function(){ return __stack[1].getLineNumber(); } }); console.log(__line); The above will log 19. Combined with arguments.callee.caller you can get … Read more

How to efficiently check if variable is Array or Object (in NodeJS & V8)?

For simply checking against Object or Array without additional function call (speed). isArray() let isArray = function(a) { return (!!a) && (a.constructor === Array); }; console.log(isArray( )); // false console.log(isArray( null)); // false console.log(isArray( true)); // false console.log(isArray( 1)); // false console.log(isArray( ‘str’)); // false console.log(isArray( {})); // false console.log(isArray(new Date)); // false console.log(isArray( [])); … Read more

How to read audio data from a ‘MediaStream’ object in a C++ addon

The MediaStream header is part of Blink’s renderer modules, and it’s not obvious to me how you could retrieve this from nan plugin. So, instead let’s look at what you do have, namely a v8::Object. I believe that v8::Object exposes all the functionality you need, it has: GetPropertyNames() Get(context, index) Set(context, key, value) Has(context, key) … Read more

Javascript Engines Advantages

There are various approaches to JavaScript execution, even when doing JIT. V8 and Nitro (formerly known as SquirrelFish Extreme) choose to do a whole-method JIT, meaning that they compile all JavaScript code down to native instructions when they encounter script, and then simply execute that as if it was compiled C code. SpiderMonkey uses a … Read more

es6 Map and Set complexity, v8 implementation

Is it a fair assumption that in v8 implementation retrieval / lookup is O(1)? Yes. V8 uses a variant of hash tables that generally have O(1) complexity for these operations. For details, you might want to have a look at https://codereview.chromium.org/220293002/ where OrderedHashTable is implemented based on https://wiki.mozilla.org/User:Jorend/Deterministic_hash_tables.