jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
What is faster when traversing each character of a string: doing a substr() or charAt() in every iteration, or converting the string to an array and back to a string?
<script>
var example = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi egestas venenatis risus nec dictum. Quisque hendrerit tellus sed ante tincidunt dapibus. Morbi semper tristique massa sed placerat. In pellentesque felis non nibh facilisis porttitor. Nullam hendrerit pharetra tortor quis luctus.";
var letters = { from: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", to: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split("") };
var translate = function(ch) {
var index = letters.from.indexOf(ch);
if (index >= 0) {
return letters.to[index];
}
return ch;
};
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
substr() in each iteration |
| ready |
charAt() in each iteration |
| ready |
Convert to array and back |
| ready |
substr() in each iteration, with concatenation |
| ready |
Convert to array and back, with push |
| ready |
Array access (for those browsers that support it) |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.