Last active
June 1, 2020 20:45
-
-
Save nooga/c7e697627b02ebb3264952125c19c376 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> Array.prototype.sort.call(x, (a,b) => { console.log(x); return a - b; }) | |
{ '0': 2, '1': 1, '2': 0, '3': -1, '7': -2, '8': -3, length: 9 } | |
{ '0': 2, '1': 1, '2': 0, '3': -1, '7': -2, '8': -3, length: 9 } | |
{ '0': 2, '1': 1, '2': 0, '3': -1, '7': -2, '8': -3, length: 9 } | |
{ '0': 2, '1': 1, '2': 0, '3': -1, '7': -2, '8': -3, length: 9 } | |
{ '0': 2, '1': 1, '2': 0, '3': -1, '7': -2, '8': -3, length: 9 } | |
{ '0': -3, '1': -2, '2': -1, '3': 0, '4': 1, '5': 2, length: 9 } | |
> x = [2,1,0,-1,,,-2,-3] | |
[ 2, 1, 0, -1, <2 empty items>, -2, -3 ] | |
> Array.prototype.sort.call(x, (a,b) => { console.log(x); return a - b; }) | |
[ 2, 1, 0, -1, <2 empty items>, -2, -3 ] | |
[ 2, 1, 0, -1, <2 empty items>, -2, -3 ] | |
[ 2, 1, 0, -1, <2 empty items>, -2, -3 ] | |
[ 2, 1, 0, -1, <2 empty items>, -2, -3 ] | |
[ 2, 1, 0, -1, <2 empty items>, -2, -3 ] | |
[ -3, -2, -1, 0, 1, 2, <2 empty items> ] | |
// wtf V8 | |
> x = [2,1,0,-1,,,-2,-3] | |
[ 2, 1, 0, -1, <2 empty items>, -2, -3 ] | |
> x.sort((a,b) => {console.log(x); return a - b; }) | |
[ 2, 1, 0, -1, <2 empty items>, -2, -3 ] | |
[ 2, 1, 0, -1, <2 empty items>, -2, -3 ] | |
[ 2, 1, 0, -1, <2 empty items>, -2, -3 ] | |
[ 2, 1, 0, -1, <2 empty items>, -2, -3 ] | |
[ 2, 1, 0, -1, <2 empty items>, -2, -3 ] | |
[ -3, -2, -1, 0, 1, 2, <2 empty items> ] | |
> x = [2,1,0,-1,,,-2,-3] | |
[ 2, 1, 0, -1, <2 empty items>, -2, -3 ] | |
> x.sort() | |
[ -1, -2, -3, 0, 1, 2, <2 empty items> ] // WTF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
according to JS spec (http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf):
Because non-existent property values always compare greater than undefined property values, and undefined always
compares greater than any other value, undefined property values always sort to the end of the result, followed by nonexistent property values.