microsoft / tslib

Runtime library for TypeScript helpers.
BSD Zero Clause License
1.24k stars 124 forks source link

Performance improvement #232

Open veksa opened 7 months ago

veksa commented 7 months ago

Based on the benchmark, the "for in" loop appears to be 10% slower than the "for" loop. Does it make sense to replace all instances of "for in"?

https://jsben.ch/kxBxv

HolgerJeromin commented 7 months ago

If we want to microbenchmark we could also do the even more performant version of your very simple test:

var sum = 0;
for (var v of Object.values(object)) {

}

But this has perhaps too bad browser support (no IE, no edge 12-13, no browser before 2016/2017): https://caniuse.com/object-values

veksa commented 7 months ago

I updated the benchmark at https://jsben.ch/IWqVX. Object.values performs significantly better, and to support incompatible browsers, we can write a polyfill function.