This repository has been archived by the owner on May 9, 2019. It is now read-only.
highly efficient solution #11
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9c12fd7
highly efficient solution
haoel 24a33aa
cover a negtive case : (len < str.length)
haoel 81ca02c
another corner case: if the "ch" is the integer we need cover it to s…
haoel 5ff6fa8
coding sytle
haoel 2fca28d
bug fixing - declare 'pad' variable as a local variable
haoel 1a90e0f
performance test
haoel 6ccfef5
change the typecasting way for 'str' and adjust the order of the code…
haoel 34c1e5c
chore: move benchmark to devdep, add bench script
zhuangya 277ebb3
Merge pull request #1 from zhuangya/minor-change
haoel 4bc455c
remove the comments
haoel 672d883
restore the comments
haoel 64f3b11
removed the commented code
haoel File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
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,69 @@ | ||
function leftpad_orginal (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; | ||
} | ||
|
||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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(); | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 abench
folder. If not I'll do it myself. Thanks.