Created
February 8, 2017 11:44
-
-
Save Tom910/037e188cdc07ad122d15bce4596761df 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
var Benchmark = require('benchmark'); | |
var expect = require('expect'); | |
var suite = new Benchmark.Suite; | |
var test = { | |
reasons: [], | |
chunks: [] | |
}; | |
for (var s = 0; s < 5000; s++) { | |
test.reasons.push({ | |
module: { chunks: [4,21,3,214,12,5,12,5]} | |
}); | |
test.chunks.push({ | |
entryModule: [1,2,3,4,5,6,7] | |
}); | |
} | |
suite | |
.add('Refactoring code', () => refactoringCode(test)) | |
.add('Old code', () => oldCode(test)) | |
.on('cycle', event => console.log(String(event.target))) | |
.on('complete', function() {console.log('Fastest is ' + this.filter('fastest').map('name'))}) | |
.run({ 'async': true }); | |
function refactoringCode(m) { | |
var result = 0; | |
var reasons = m.reasons; | |
for(var index = 0; index < reasons.length; index++) { | |
var reasonModule = reasons[index].module; | |
if(!reasonModule) continue; | |
result = result + reasonModule.chunks.length; | |
} | |
var chunks = m.chunks; | |
for(var i = 0; i < chunks.length; i++) { | |
if(chunks[i].entryModule === m) { | |
result++; | |
} | |
} | |
return result + m.chunks.length; | |
} | |
function oldCode(m) { | |
const result = m.reasons.map((r) => { | |
if(!r.module) return 0; | |
return r.module.chunks.length; | |
}).reduce((a, b) => { | |
return a + b; | |
}, 0) + m.chunks.length + m.chunks.filter((c) => { | |
return c.entryModule === m; | |
}).length; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment