Created
January 19, 2017 00:36
-
-
Save PuercoPop/12dfd83a72f0a14cef9577ed839e2896 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// time sortNumbers.js => 0.196 | |
function randomInteger(lowerBound, upperBound) { | |
return lowerBound + Math.floor(Math.random() * (upperBound - lowerBound)); | |
} | |
function* randomNumbers(count) { | |
for (var i=0; i < count; i++) { | |
yield randomInteger(1, 10); | |
} | |
} | |
function cmp(a, b) { | |
return a - b; | |
} | |
var a = Array.from(randomNumbers(100000)); | |
console.time("sort") | |
a.sort(cmp); | |
console.timeEnd("sort") // => 10ms |
This file contains hidden or 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
"use strict"; | |
var idCount = 1; | |
function nextId() { | |
return idCount++; | |
} | |
function randomString() { | |
return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5); | |
return "a"; | |
} | |
function randomInteger(lowerBound, upperBound) { | |
return lowerBound + Math.floor(Math.random() * (upperBound - lowerBound)); | |
} | |
function cmpPersons(a, b) { | |
return a.age - b.age; | |
} | |
class Person { | |
constructor() { | |
this.id = nextId(); | |
this.name = randomString(); | |
this.age = randomInteger(1, 100); | |
} | |
} | |
function* randomPersons(count) { | |
for (var i=0; i < count; i++) { | |
yield new Person(); | |
} | |
} | |
var ps = Array.from(randomPersons(100000)) | |
console.time("sort") | |
ps.sort(cmpPersons); | |
console.timeEnd("sort") // => 33ms | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment