paulmillr / es6-shim

ECMAScript 6 compatibility shims for legacy JS engines
http://paulmillr.com
MIT License
3.11k stars 389 forks source link

optimal performance for looping Map #242

Closed debuggingfuture closed 10 years ago

debuggingfuture commented 10 years ago

what is the most optimal way in terms of performance to loop the Map in es6 shim? I am trying to use .forEach http://jsperf.com/es6-shim

it seems it has larger penalty of index vs reference traversal than usual, probably as data did not benefit from array locality? http://jsperf.com/javascript-graph/2

ljharb commented 10 years ago

Since it's a shim, performance isn't the goal, compliance is. That said, if you find a particular part of the Map shim to be slow, we'd love to optimize it.

Maps and Sets in the shim have both a fast and a slow path - basically, the "fast path" is for non-objects. Your test case doesn't include objects, so it only hits the fast path - and if you're not using objects, you might as well just use a normal {} :-)

Also, the "native array" test is irrelevant - you should be comparing to something like Object.keys(map).forEach(function (key) { map[key] }) instead.

Yaffle commented 10 years ago

http://jsperf.com/es6-shim/2

Yaffle commented 10 years ago

it is ineresting, that Map.prototype.forEach in es6-shim is faster than native in FF 28 and Chrome 26, and slower in IE 11 only

ljharb commented 10 years ago

To reiterate, we'll be happy to look at performance-oriented PRs - however, "non-optimal performance" isn't an issue for this shim, so I'm closing this.