slow function call in V8 when using the same key for the functions in different objects

First fundamentals. V8 uses hidden classes connected with transitions to discover static structure in the fluffy shapeless JavaScript objects. Hidden classes describe the structure of the object, transitions link hidden classes together describing which hidden class should be used if a certain action is performed on an object. For example the code below would lead … Read more

Do common JavaScript implementations use string interning?

Yes. In general any literal string, identifier, or other constant string in JS source is interned. However implementation details (exactly what is interned for instance) varies, as well as when the interning occurs. Note that a string value is not the same as a String Object though, String Objects are not interned because that would … Read more

Different results using the same maths in different browsers

The problem is not with the Javascript math; it’s with the canvas. http://jsfiddle.net/LDWBX/ function bigCircle(angle) { var radius = 5000; //the bigger, the worse var x = canvas.width/2 + radius*Math.cos(angle); var y = canvas.height/2 + radius*Math.sin(angle); ctx.beginPath(); ctx.arc(x, y, radius, 0, 2 * Math.PI); ctx.lineWidth = 2; ctx.stroke(); } Notice that numbers appear exactly the … Read more

How to run user-submitted scripts securely in a node.js sandbox?

You should always run untrusted code in a separate process, which is exactly what the sandbox module does. A simple reason is that vm.runInNewContext(‘while(true){}’, {}) will freeze node. It starts by spawning a separate process, which will later send the result serialized to JSON on its stdout. The parent process continues executing regardless of what … Read more

Why doesn’t Node.js have a native DOM?

The DOM is the DOM, and the JavaScript implementation is simply a separate entity. The DOM represents a set of facilities that a web browser exposes to the JavaScript environment. There’s no requirement however that any particular JavaScript runtime will have any facilities exposed via the global object. What Node.js is is a stand-alone JavaScript … Read more

How can I check if a JSON is empty in NodeJS?

You can use either of these functions: // This should work in node.js and other ES5 compliant implementations. function isEmptyObject(obj) { return !Object.keys(obj).length; } // This should work both there and elsewhere. function isEmptyObject(obj) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { return false; } } return true; } Example usage: if … Read more

Why is the execution time of this function call changing?

V8 developer here. It’s not a bug, it’s just an optimization that V8 doesn’t do. It’s interesting to see that Firefox seems to do it… FWIW, I don’t see “ballooning to 400ms”; instead (similar to Jon Trent’s comment) I see about 2.5ms at first, and then around 11ms. Here’s the explanation: When you click only … Read more