fastpad

Fastest pad left implementation, 2 millions ops in 48ms.

Usage no npm install needed!

<script type="module">
  import fastpad from 'https://cdn.skypack.dev/fastpad';
</script>

README

FastPad

FastPad is a blazing fast left-pad function with performances better than pad-left and the new native function repeat (Present in the new ES6 and Node 6).

How is it possible to be faster than pad-left, the one used by google and so on?

The algorithm is the Egyptian string concatenation algorithm found in the British museum by researchers a while ago.

Even though pad-left is very fast, I've done it better by caching the result as well as there is no need to redo the math if the input is the same as the cached input.

How to use

$ npm install fastpad --save 

var pad = require('fastpad');

console.log(pad('0',4,'4'));

On a small scale, the execution time for a single record is 5 microseconds compared to 6 microseconds which the famous pad-left has.

Below you can see some benchmark results ( each test was ran 100 times ):

PAD That returns 0004

pad('0',4,4) Result: 2.000.000 ops /  48 miliseconds
pad('0',4,'4') Result: 20.000.000 ops / 439 miliseconds
pad('0',4,4) Result: 200.000.000 ops / 3.9 seconds

PAD That returns 000000test

pad('0',10,'test') Result: 200.000.000 ops / 4.2 seconds

Author - Alexandru-Mihai Spinu