Skip to content

Commit

Permalink
refactor: better perf support
Browse files Browse the repository at this point in the history
this should finish what @stevemao said in left-pad#11
  • Loading branch information
zhuangya committed Apr 17, 2016
1 parent 64f3b11 commit 8fc086a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 70 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -5,10 +5,11 @@
"main": "index.js",
"scripts": {
"test": "node test",
"bench": "node perf_test.js"
"bench": "node perf/perf.js"
},
"devDependencies": {
"benchmark": "^2.1.0",
"microtime": "^2.0.0",
"tape": "*"
},
"keywords": [
Expand Down
19 changes: 19 additions & 0 deletions perf/es6Repeat.js
@@ -0,0 +1,19 @@
'use strict';

module.exports = function leftpadEs6Repeat (str, len, ch) {
var l;

str = String(str);
l = len - str.length;

if (!ch && ch !== 0) {
ch = ' ';
}

if (l > 0) {
return ch.repeat(l) + str;
}

return str;

};
20 changes: 20 additions & 0 deletions perf/original.js
@@ -0,0 +1,20 @@
'use strict';

module.exports = function leftpadOriginal (str, len, ch) {
str = String(str);

var i = -1;

if (!ch && ch !== 0) {
ch = ' ';
}

len = len - str.length;

while (++i < len) {
str = ch + str;
}

return str;
}

38 changes: 38 additions & 0 deletions perf/perf.js
@@ -0,0 +1,38 @@
'use strict';


var leftpadOriginal = require('./original');
var leftpadEs6Repeat = require('./es6Repeat');
var leftpadBitOps = require('../');

var Benchmark = require('benchmark');

var str = "abcd"
var len = 100;

function buildSuite (note, methods, parameters) {
console.log(note);
var newSuite = new Benchmark.Suite;

Object.keys(methods).forEach(function (name) {
newSuite.add(name, function () {
methods[name].apply(null, parameters);
});
});
newSuite.on('cycle', function (event) {
console.log(String(event.target));
}).on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'));
});

return newSuite;
}

var methods = {
'Long - Original': leftpadOriginal,
'Long - ES6 Repeat': leftpadEs6Repeat,
'Long - Bit Operation': leftpadBitOps
};

buildSuite('-> len = 100', methods, ['abcd', 100, ' ']).run();
buildSuite('-> len = 10', methods, ['abcd', 10, ' ']).run();
69 changes: 0 additions & 69 deletions perf_test.js

This file was deleted.

0 comments on commit 8fc086a

Please sign in to comment.