Last active
August 29, 2015 13:59
-
-
Save furf/10983299 to your computer and use it in GitHub Desktop.
Compare sort speeds using inline and pre-computed distance calculations.
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
// Return a color object | |
function color(r, g, b) { | |
return { | |
r: r, | |
g: g, | |
b: b | |
}; | |
} | |
// Return a random number between a and b inclusive | |
function random(a, b) { | |
return Math.floor(a + Math.random() * (b + 1 - a)); | |
} | |
// Return a random hex value | |
function randomHex() { | |
return random(0, 255); | |
} | |
// Generate a color using random values | |
function randomColor() { | |
return color(randomHex(), randomHex(), randomHex()); | |
} | |
// Create an array of random colors of length n | |
function randomColors(n) { | |
var colors = new Array(n); | |
while (n--) { | |
colors[n] = randomColor(); | |
} | |
return colors; | |
} | |
// Pretty stringify | |
function $$(obj) { | |
return JSON.stringify(obj, 0, 4); | |
} | |
// Calculate distance between two colors | |
function getDistance(a, b) { | |
var r = b.r - a.r; | |
var g = b.g - a.g; | |
var b = b.b - a.b; | |
return r * r + g * g + b * b; | |
} | |
// Sort color distance, calculating distance in the loop | |
function sortByColorSlow(c, colors) { | |
return colors.sort(function(a, b) { | |
var aD = getDistance(a, c); | |
var bD = getDistance(b, c); | |
return aD - bD; | |
}); | |
} | |
// Sort color distance, precomputing distance outside of the loop | |
function sortByColorFast(c, colors) { | |
var i; | |
var n = colors.length; | |
for (i = 0; i < n; ++i) { | |
colors[i]._d = getDistance(c, colors[i]); | |
} | |
colors.sort(function(a, b) { | |
return a._d - b._d; | |
}); | |
// Deleting slows this way down | |
// If you don't like adding this property, | |
// sort a lookup table instead. | |
// for (i = 0; i < n; ++i) { | |
// delete colors[i]._d; | |
// } | |
return colors; | |
} | |
// Count iterations in given time | |
function _test(fn) { | |
var then = Date.now(); | |
var i = 0; | |
do { | |
fn(); | |
i++ | |
} while ((Date.now() - then) < 1000); | |
return i; | |
} | |
// Average performance of multiple tests | |
function test(fn) { | |
var i = 0; | |
var n = 3; | |
var speed = 0; | |
for (; i < n; ++i) { | |
speed = speed + _test(fn); | |
} | |
return (speed / n).toFixed(1); | |
} | |
// TESTING TIME | |
// Build catalog | |
var colors = randomColors(100); | |
// Create random query | |
var search = randomColor(); | |
// Test slow sort | |
var slow = test(function() { | |
sortByColorSlow(search, colors.slice()); | |
}, duration); | |
// Test fast sort | |
var fast = test(function() { | |
sortByColorFast(search, colors.slice()); | |
}, duration); | |
console.log('slow', slow); | |
console.log('fast', fast); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment