Skip to content

Instantly share code, notes, and snippets.

@ramntry
Last active December 28, 2015 18:49
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 ramntry/7545613 to your computer and use it in GitHub Desktop.
Save ramntry/7545613 to your computer and use it in GitHub Desktop.
var arr = [];
for (var i = 0, l = 100*1000; i < l; i++) {
arr.push(Math.round(Math.random() * l))
}
Array.prototype.diff = function(a) {
return this.filter(function(i) {return !(a.indexOf(i) > -1);});
};
for (var i = 0; i < 10; ++i) {
console.log(arr.diff(arr).length == 0);
}
var arr = [];
for (var i = 0, l = 100*1000; i < l; i++) {
arr.push(Math.round(Math.random() * l))
}
Array.prototype.hasAll = function(a) {
var hash = this.reduce(function(acc, i) { acc[i] = true; return acc; }, {});
return a.every(function(i) { return i in hash; });
};
for (var i = 0; i < 10; ++i) {
console.log(arr.hasAll(arr));
}
@ramntry
Copy link
Author

ramntry commented Nov 19, 2013

ramntry@ramntry-R418:~/studies/javascript/hasAll$ time nodejs ramntry.js
true
true
true
true
true
true
true
true
true
true

real 0m0.528s
user 0m0.504s
sys  0m0.028s
ramntry@ramntry-R418:~/studies/javascript/hasAll$ time nodejs egor_nullptr.js 
true
true
true
true
true
true
true
true
true
true

real 1m47.103s
user 1m47.951s
sys  0m0.600s
ramntry@ramntry-R418:~/studies/javascript/hasAll$ nodejs --version
v0.10.22
ramntry@ramntry-R418:~/studies/javascript/hasAll$ cat /proc/cpuinfo
processor   : 0
-//-
model name  : Intel(R) Pentium(R) Dual  CPU  T3400  @ 2.16GHz
-//-
cache size  : 1024 KB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment