Created
April 21, 2014 12:38
-
-
Save mizchi/11141597 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
uuid = -> ~~(Math.random() * 100000) | |
n = 1000000 | |
list = | |
for i in [1..n] | |
id: uuid | |
find = (list, fn) -> | |
for i in list | |
ret = fn(i) | |
if ret then return i | |
null | |
find_by_id = (list, id) -> | |
for i in list | |
if i.id is id then return i | |
null | |
find_with_indexes = (list, indexes, id) -> | |
index = indexes[id] | |
return list[index] | |
create_indexes = (list, key) -> | |
indexes = {} | |
for i, n in list | |
indexes[i[key]] = n | |
try_count = 10 | |
console.time 'find' | |
for i in [1..try_count] | |
find list, (i)-> i.id is uuid() | |
console.timeEnd 'find' | |
console.time 'find_by_id' | |
for i in [1..try_count] | |
find_by_id list, uuid() | |
console.timeEnd 'find_by_id' | |
id_indexes = create_indexes list, 'id' | |
console.time 'find_with_indexes' | |
for i in [1..try_count] | |
find_with_indexes list, id_indexes, uuid() | |
console.timeEnd 'find_with_indexes' |
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
$ coffee find_performance.coffee | |
find: 158ms | |
find_by_id: 81ms | |
find_with_indexes: 0ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment