JavaScript Algorithms and JavaScript Engines

191 Views Asked by At

Is algorithms for JavaScript functions on different JavaScript engine varies?

I run Array.sort() in Chrome and Firefox browser, and two of them perform differently for the same code.

Are they used different algorithms for the same code?

1

There are 1 best solutions below

2
On

All that's guaranteed by the specification is a non-stable in-place sorting method.

So yes, different implementations are allowed to use different algorithms, and because the specification does not guarantee a stable algorithm, different non-stable algorithms will result in different orders if your array values evaluate as equal, which is any falsy return value from the compare function you pass to Array.prototype.sort().