forked from haoel/left-pad
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
this should finish what @stevemao said in left-pad#11
- Loading branch information
Showing
5 changed files
with
79 additions
and
70 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
This file was deleted.
Oops, something went wrong.