Skip to content
This repository has been archived by the owner on May 9, 2019. It is now read-only.

highly efficient solution #11

Closed
wants to merge 12 commits into from
22 changes: 14 additions & 8 deletions index.js
@@ -1,17 +1,23 @@
module.exports = leftpad;

function leftpad (str, len, ch) {
str = String(str);
//convert the `str` to String
str = str +'';

var i = -1;
//needn't to pad
len = len - str.length;
if (len <= 0) return str;

//convert the `ch` to String
if (!ch && ch !== 0) ch = ' ';
ch = ch + '';

len = len - str.length;

while (++i < len) {
str = ch + str;
var pad = '';
while (true) {
if (len & 1) pad += ch;
len >>= 1;
if (len) ch += ch;
else break;
}

return str;
return pad + str;
}
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -4,9 +4,11 @@
"description": "String left pad",
"main": "index.js",
"scripts": {
"test": "node test"
"test": "node test",
"bench": "node perf_test.js"
},
"devDependencies": {
"benchmark": "^2.1.0",
"tape": "*"
},
"keywords": [
Expand Down
69 changes: 69 additions & 0 deletions perf_test.js
@@ -0,0 +1,69 @@
function leftpad_orginal (str, len, ch) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I've been busy with other stuff. If you have time could you put this function in a standalone js file and name it O(n)? Similar to the functions below. and put them in a bench folder. If not I'll do it myself. Thanks.

str = String(str);

var i = -1;

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

len = len - str.length;

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

return str;
}


function leftpad_es6_repeat(str, len, ch) {
str = String(str);
if (!ch && ch !== 0) ch = ' ';
var l = len - str.length;
if (l > 0) return ch.repeat(l)+str;
return str;
}



function leftpad_bit_ops(str, len, ch) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require('./')

str = String(str);

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

len = len - str.length;
if (len <= 0) return str;

ch = ch + '';
var pad = '';
while (true) {
if (len & 1) pad += ch;
len >>= 1;
if (len) ch += ch;
else break;
}
return pad + str;
}


var Benchmark = require('benchmark');

var t_str = "abcd"
var t_len = 100;

var suite01 = new Benchmark.Suite;
suite01.add('Long - Original', function() { leftpad_orginal(t_str, t_len, ' ');})
.add('Long - ES6 Repeat', function() { leftpad_es6_repeat(t_str, t_len, ' ');})
.add('Long - Bit Operation', function() { leftpad_bit_ops(t_str, t_len, ' ');})
.on('cycle', function(event) { console.log(String(event.target)); })
.on('complete', function() { console.log('Fastest is ' + this.filter('fastest').map('name')); })
.run();

t_len=10;
var suite02 = new Benchmark.Suite;
suite02.add('Normal - Original', function() { leftpad_orginal(t_str, t_len, ' ');})
.add('Normal - ES6 Repeat', function() { leftpad_es6_repeat(t_str, t_len, ' ');})
.add('Normal - Bit Operation', function() { leftpad_bit_ops(t_str, t_len, ' ');})
.on('cycle', function(event) { console.log(String(event.target)); })
.on('complete', function() { console.log('Fastest is ' + this.filter('fastest').map('name')); })
.run();

5 changes: 4 additions & 1 deletion test.js
Expand Up @@ -2,9 +2,12 @@ var leftpad = require("./");
var test = require("tape");

test('left pad', function (assert) {
assert.plan(4);
assert.plan(7);
assert.strictEqual(leftpad('foo', 5), ' foo');
assert.strictEqual(leftpad('foobar', 6), 'foobar');
assert.strictEqual(leftpad(1, 2, 0), '01');
assert.strictEqual(leftpad(1, 2, '-'), '-1');
assert.strictEqual(leftpad('foo', 2, ' '), 'foo');
assert.strictEqual(leftpad('foo', -1, ' '), 'foo');
assert.strictEqual(leftpad('foo', 7, 1), '1111foo');
});