Skip to content

Instantly share code, notes, and snippets.

@billhance
Last active December 9, 2016 22:03
Show Gist options
  • Save billhance/b576db4a58b2c3ccbe67df07acf4cc1f to your computer and use it in GitHub Desktop.
Save billhance/b576db4a58b2c3ccbe67df07acf4cc1f to your computer and use it in GitHub Desktop.
v8-devtools-bug
// Run it here: https://jsfiddle.net/billhance/wfz9fcs6/1/
const selectionSortSlow = arr => {
let curr = 0;
let n = arr.length;
while (curr < n) {
let low = curr;
let next = curr + 1;
while (next < n) {
if (arr[next] > arr[low]) {
low = next;
}
next++;
}
// tmp is declared inside the outer loop
let tmp = arr[curr];
arr[curr] = arr[low];
arr[low] = tmp;
curr++;
}
};
const selectionSortFast = arr => {
let curr = 0;
let n = arr.length;
// tmp is declared outside of the loop
let tmp = null;
while (curr < n) {
let low = curr;
let next = curr + 1;
while (next < n) {
if (arr[next] > arr[low]) {
low = next;
}
next++;
}
tmp = arr[curr];
arr[curr] = arr[low];
arr[low] = tmp;
curr++;
}
};
const selectionSortFastest = arr => {
let curr = 0;
let n = arr.length;
while (curr < n) {
let low = curr;
let next = curr + 1;
while (next < n) {
if (arr[next] > arr[low]) {
low = next;
}
next++;
}
// Wrap the swap with arbitrary block scoping
{
let tmp = arr[curr];
arr[curr] = arr[low];
arr[low] = tmp;
curr++;
}
}
};
console.profile("selectionSortSlow");
selectionSortSlow([...Array(10000).keys()]);
console.profileEnd();
console.profile("selectionSortFast");
selectionSortFast([...Array(10000).keys()]);
console.profileEnd();
console.profile("selectionSortFastest");
selectionSortFastest([...Array(10000).keys()]);
console.profileEnd();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment