Zero Padding
JavaScript performance comparison
Info
This tests various solutions to front zero pad a number. In my tests I only want to test for a set number of desired number length which is a two digit number (e.g. 2 becomes 02, 10 stays 10).
Preparation code
<script>
var h = 5;
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Ternary Padding (as string) |
|
pending… |
Slice() Padding (as string) |
|
pending… |
Slice() Padding (as array) |
|
pending… |
Ternary Padding (as array) |
|
pending… |
Function based (complex) |
|
pending… |
Function based (simple) |
|
pending… |
If Padding |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published by Milan Adamovsky
- Revision 2: published
- Revision 3: published
- Revision 4: published
- Revision 6: published by Redger
- Revision 8: published
4 comments
This is just to test our Manso's trick.
Credit: http://www.codigomanso.com/en/2010/07/simple-javascript-formatting-zero-padding/
Awesome, is really good to have tests to check performance issues of javascript code right on the browser, available to anyone ;)
As suggested by Ola recently on the thread of my post, here is a piece of code worth considering:
This will produce "003" which is more understandable than other approaches.