News for hackers. Stuff that matters.
Re:How JavaScript works: inside the V8 engine by inglor on Monday August 21, 2017 @01:41PM (#15066187)
In V8, objects are actually arrays (sort of, they have an indexed store) - so doing `this[0]` is about as fast as doing it with an array (certain array things like for... of iteration are optimized though - but only if you haven't changed the prototype).
Re:How JavaScript works: inside the V8 engine by avaer on Monday August 21, 2017 @01:58PM (#15066302)
In case it wasn't clear, when I said prefer typed arrays over objects, I literally meant Typed Arrays [1].

These are not like arrays _or_ objects. They're faster than either, since there are less corner cases and type ambiguities.

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Type...

Re:How JavaScript works: inside the V8 engine by fenomas on Monday August 21, 2017 @02:06PM (#15066372)
Is that definitely true? Some time ago I was tuning code that did lots of math on a big array of floats, and I found that using Float32Arrays was moderately but measurably slower than just using [].

This may have been before TurboFan, though.

Re:How JavaScript works: inside the V8 engine by zbjornson on Monday August 21, 2017 @05:12PM (#15067897)
You're right. In V8, values from a Float32Array are always converted to doubles upon access, even in cases like adding values from two Float32Arrays and storing them in another Float32Array where the result is provably the same. That costs a handful of cycles for each element.

Mozilla has a blog post about doing efficient float32 math: https://blog.mozilla.org/javascript/2013/11/07/efficient-flo...

V8 bug: https://bugs.chromium.org/p/v8/issues/detail?id=5541

Using a Float64Array is faster at the expense of memory usage. (I've never seen a case where the code generated by V8 bottlenecks on memory bandwidth fwiw.)

Re:How JavaScript works: inside the V8 engine by fenomas on Monday August 21, 2017 @09:52PM (#15069159)
Thanks for the details, very useful!

I don't suppose you know if similar things are true for Int arrays? I think I once had a similar case there, where I was doing integer operations on a large set of ints, and regular arrays were faster than Int32. But it's hard to imagine there as well what the slowdown would be.

Re:How JavaScript works: inside the V8 engine by zbjornson on Tuesday August 22, 2017 @12:31AM (#15069720)
Good question. I haven't looked at that case. V8 can detect and optimize small ints (smi, fit in uint32, i.e. all types of typedarrays), but it might still take the easy/slow path and convert to doubles. Relatively easy to look at with node.js and irhydra.
Re:How JavaScript works: inside the V8 engine by mnutt on Monday August 21, 2017 @03:24PM (#15067017)
In a few different cases (one node, one browser-based) I've had medium-sized read-only datasets of a couple million objects where I've tried to conserve memory by using typed arrays. While it saved a good amount of memory, property access was significantly faster via objects than typed views. I'm curious to re-run the benchmarks with Turbofan though, maybe things have changed.
Re:How JavaScript works: inside the V8 engine by avaer on Monday August 21, 2017 @03:34PM (#15067125)
Indeed, it heavily depends on what your JS engine thinks your data types are, your allocation patterns, and more. If your engine thinks you're using floats to index into a typed array that might be slower than using floats to index into an object. Even if you know your numbers are really only integers. Which is why JS performance is so puzzling sometimes.

One thing you can do with typed arrays that you cannot do with objects, though, is zero-copy into workers. You can use that technique to get pretty much free parallel performance on any number-crunchy task.

These things change all the time though; all the more reason for always measuring before you cut and investing into better profiling tools.

Re:How JavaScript works: inside the V8 engine by nopenopenope8 on Monday August 21, 2017 @01:52PM (#15066256)
This is incorrect. Arrays are implemented as a type of object, but you shouldn't worry about that. Hashmaps and arrays both have ~O(1) random indexing. If you need an array, don't use an object.
Re:How JavaScript works: inside the V8 engine by inglor on Monday August 21, 2017 @02:07PM (#15066375)
Objects have a length overhead. At Bluebird we used objects and used a bitmask on the length so it's fast. (We use less slots than an array actually)

I was just pointing out that Objects weren't slow for indexed access.