3 ms·
It seems notable that while map is mentioned there is no performance measurement shown for map. To me The proof of the pudding would be in map performance. I
by Lerc 7y ago
It seems notable that while map is mentioned there is no performance measurement shown for map. To me The proof of the pudding would be in map performance.
In my spare time I have been building various versions of the nbody benchmark to try out different fundamental data methods, arrays, objects, immutable or not etc. Every alternative version is, of course, slower than the original mutating object version. In principle however, many forms should be optimisable by a 'sufficiently smart JIT' to be at least as fast.
It would be nice at least for a JavaScript engine to be able to do in-place maps on arrays defined from literals or single creation points like map/filter etc. , and have only one copy.
For instance.
{
let x = [1,5,9];
let q = x.map(a=>a+1)
}
The JIT would have to determine that
. x held the only reference to the array.
. x.map is the last reference to x
. the array in x was not modified between creation and last reference.
. the used version of a=>a+1 returns the same type as it is passed
To place the result of the map into the same memory as the original array and assign that to q.
Of course in this ultra simple example an optimizer could just figure out that q is [2,6,10] and never used anyway so throw it away. but in real world code you can frequently have arrays like this that are only ever used as a source for a map, especially if you do q.map(something).map(somethingelse).map(anotherthing).
- gpderetta 7y agoFunctional language compilers are sometimes capable to eliminate intermediate lists, and possibly reuse the original list storage. IIRC the optimization is called deforestation.
- lioeters 7y agoI found the benchmark mentioned in the article - here's the part comparing map: https://funkia.github.io/list/benchmarks#map https://funkia.github.io/list/benchmarks#map Apparently, List is ~3x faster than native array.map, with the difference increasing with the number of elements. I see that random access and iterator are two operations where native array is fastest.