Unstable .sort()
Only with some vendors, go figure.
HTML
<link rel="stylesheet" href="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css">
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>
<title>Jasmine Spec Runner</title>
JavaScript
// Be careful sorting arrays when you have multiple "equal" values in JS!
// Special thanks to Chromium team for this vendor landmine
// in the standard library. When you find weasel words like
// "not necessarily stable" in
// https://www.ecma-international.org/ecma-262/7.0/#sec-array.prototype.sort
// you should opt for what leads to the least surprises for
// developers across vendors, not what makes you slightly
// faster than the competition.
// And to the person who wrote that into the spec: Bullshit
// like this makes the language suck. The same code executed
// with the same input across different engines should be
// guaranteed to result in the same output. You gave Google
// an excuse to play in undefined behavior and they are
// stubbornly refusing to change.
// Yeah, there are workarounds (like DIY), but nobody likes
// GOTCHAs.
// This is a trivial example, but for where you're sorting
// [{type: 'thing', id: 1}, ...], on type where it might
// be repeated, you *may* get unexpected sorting like shown.
describe("Array sort stability is your friend", () => {
it("Except if you're Chrome apparently", () => {
let original = Array(100).fill(0).map((_, idx) => idx);
let noChangeSort = original.slice().sort(() => 0);
expect(noChangeSort).toEqual(original);
})
})
var env = jasmine.getEnv();
env.addReporter(new jasmine.HtmlReporter());
env.execute();