Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created April 21, 2014 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizchi/11141597 to your computer and use it in GitHub Desktop.
Save mizchi/11141597 to your computer and use it in GitHub Desktop.
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'
$ 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