Created
January 7, 2017 11:05
-
-
Save panetta-net-au/ebf42b169ae59a80f9e4aa015f41e269 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
var keys = []; | |
var results = []; | |
var keyCount = 100; | |
var iterations = 10000; | |
//generate keys 0 to <keycount> | |
for (var i=0; i < keyCount; i++) { | |
keys.push(i); | |
results.push({ id: i }) | |
} | |
//order keys randomly | |
keys.sort(k => Math.random()-0.5); | |
//non-sorted keys requiring reconciliation | |
var startDate = new Date(); | |
for (var j=0; j < iterations; j++) { | |
var orderedResults = []; | |
for (var i=0; i < keys.length; i++) { | |
orderedResults.push(results.filter(r => r.id == keys[i])[0]); | |
} | |
} | |
console.log('Non-sorted: ' + (new Date() - startDate)); | |
//sorted keys | |
startDate = new Date(); | |
var sortFunction = (a, b) => a.id-b.id; | |
for (var j=0; j < iterations; j++) { | |
keys.sort(sortFunction); | |
results.sort(sortFunction); | |
} | |
console.log('----Sorted: ' + (new Date() - startDate)); | |
//Sample output: | |
// >node index.js | |
// Non-sorted: 2428 | |
// ----Sorted: 256 | |
// >node index.js | |
// Non-sorted: 1980 | |
// ----Sorted: 248 | |
// >node index.js | |
// Non-sorted: 1963 | |
// ----Sorted: 273 | |
// >node index.js | |
// Non-sorted: 1924 | |
// ----Sorted: 241 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment