Created
June 3, 2011 04:08
-
-
Save satyr/1005856 to your computer and use it in GitHub Desktop.
WeakMap vs Array::indexOf on deduplication
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
uniqMap: 25ms | |
uniqOld: 337ms | |
-- | |
on Firefox 7.01 |
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
function uniqMap array | |
map = new WeakMap | |
dic = Object.create null | |
for item of array | |
type = typeof item | |
if item and type of <[ object function ]> | |
continue if map.has item | |
map.set item | |
else | |
continue if dic[key = item + type] | |
dic[key] = 1 | |
item | |
function uniqOld array | |
results = [] | |
results.push item if 0 > results.indexOf item for item of array | |
results | |
uniques = [true false] | |
uniques.push C i for i to 99 for C of [Object, Array, RegExp, Number, String] | |
duplicates = [...uniques] * 99 | |
for uniq of [uniqMap, uniqOld] | |
start = Date.now! | |
results = uniq duplicates | |
console.log "#{uniq.name}: #{ Date.now! - start }ms" | |
console.error uniques, results unless uneval(uniques) is uneval(results) |
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
var uniques, C, i, duplicates, uniq, start, results, _i, _ref, _len, __slice = [].slice; | |
function uniqMap(array){ | |
var map, dic, item, type, key, _i, _len, _results = []; | |
map = new WeakMap; | |
dic = Object.create(null); | |
for (_i = 0, _len = array.length; _i < _len; ++_i) { | |
item = array[_i]; | |
type = typeof item; | |
if (item && (type === 'object' || type === 'function')) { | |
if (map.has(item)) { | |
continue; | |
} | |
map.set(item); | |
} else { | |
if (dic[key = item + type]) { | |
continue; | |
} | |
dic[key] = 1; | |
} | |
_results.push(item); | |
} | |
return _results; | |
} | |
function uniqOld(array){ | |
var results, item, _i, _len; | |
results = []; | |
for (_i = 0, _len = array.length; _i < _len; ++_i) { | |
item = array[_i]; | |
if (0 > results.indexOf(item)) { | |
results.push(item); | |
} | |
} | |
return results; | |
} | |
uniques = [true, false]; | |
for (_i = 0, _len = (_ref = [Object, Array, RegExp, Number, String]).length; _i < _len; ++_i) { | |
C = _ref[_i]; | |
for (i = 0; i <= 99; ++i) { | |
uniques.push(C(i)); | |
} | |
} | |
duplicates = __repeatArray(__slice.call(uniques), 99); | |
for (_i = 0, _len = (_ref = [uniqMap, uniqOld]).length; _i < _len; ++_i) { | |
uniq = _ref[_i]; | |
start = Date.now(); | |
results = uniq(duplicates); | |
console.log(uniq.name + ": " + (Date.now() - start) + "ms"); | |
if (uneval(uniques) !== uneval(results)) { | |
console.error(uniques, results); | |
} | |
} | |
function __repeatArray(arr, n){ | |
for (var r = []; n > 0; (n >>= 1) && (arr = arr.concat(arr))) | |
if (n & 1) r.push.apply(r, arr); | |
return r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment