Skip to content

Instantly share code, notes, and snippets.

@leizongmin
Created September 15, 2015 06:25
Show Gist options
  • Save leizongmin/e8319f69f6bf37a46c24 to your computer and use it in GitHub Desktop.
Save leizongmin/e8319f69f6bf37a46c24 to your computer and use it in GitHub Desktop.
function fuck1 () {
function readdir (dir) {
return ['a', 'b', 'c', 'd', 'e'].map(function (n) {
return dir + n;
}).filter(function (n) {
return n;
});
}
function addTo (obj, list) {
list.forEach(function (n) {
obj[n] = true;
});
}
var ret = {};
addTo(ret, readdir('a'));
addTo(ret, readdir('b'));
return ret;
}
function readdir (dir) {
return ['a', 'b', 'c', 'd', 'e'].map(function (n) {
return dir + n;
}).filter(function (n) {
return n;
});
}
function addTo (obj, list) {
list.forEach(function (n) {
obj[n] = true;
});
}
function fuck2 () {
var ret = {};
addTo(ret, readdir('a'));
addTo(ret, readdir('b'));
return ret;
}
function MB (n) {
return (n / 1024 / 1024).toFixed(1);
}
function test (title, times, fn) {
var start = Date.now();
var heapStart = process.memoryUsage().heapUsed;
for (var i = 0; i < times; i++) {
fn();
}
var end = Date.now();
var heapEnd = process.memoryUsage().heapUsed;
console.log('%s, %s times, spent %sms, heapUsed %sMB to %sMB', title, times, end - start, MB(heapStart), MB(heapEnd));
}
test('fuck1', 1000000, fuck1);
test('fuck2', 1000000, fuck2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment