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
Testing "trim-native" and "trim27" (from rev7) with the following trim functions:
<script>
var WHITESPACE_ = [];
WHITESPACE_[0x0009] = true;
WHITESPACE_[0x000a] = true;
WHITESPACE_[0x000b] = true;
WHITESPACE_[0x000c] = true;
WHITESPACE_[0x000d] = true;
WHITESPACE_[0x0020] = true;
WHITESPACE_[0x0085] = true;
WHITESPACE_[0x00a0] = true;
WHITESPACE_[0x1680] = true;
WHITESPACE_[0x180e] = true;
WHITESPACE_[0x2000] = true;
WHITESPACE_[0x2001] = true;
WHITESPACE_[0x2002] = true;
WHITESPACE_[0x2003] = true;
WHITESPACE_[0x2004] = true;
WHITESPACE_[0x2005] = true;
WHITESPACE_[0x2006] = true;
WHITESPACE_[0x2007] = true;
WHITESPACE_[0x2008] = true;
WHITESPACE_[0x2009] = true;
WHITESPACE_[0x200a] = true;
WHITESPACE_[0x200b] = true;
WHITESPACE_[0x2028] = true;
WHITESPACE_[0x2029] = true;
WHITESPACE_[0x202f] = true;
WHITESPACE_[0x205f] = true;
WHITESPACE_[0x3000] = true;
var trim_yesudeep = function(str){
var len = str.length;
if (len){
var i = 0;
while (WHITESPACE_[str.charCodeAt(--len)]);
if (++len){
while (WHITESPACE_[str.charCodeAt(i)]){ ++i; }
}
str = str.substring(i, len);
}
return str;
};
function trim27(str) {
var c;
for (var i = 0; i < str.length; i++) {
c = str.charCodeAt(i);
if (c == 32 || c == 10 || c == 13 || c == 9 || c == 12) continue; else break;
}
for (var j = str.length - 1; j >= i; j--) {
c = str.charCodeAt(j);
if (c == 32 || c == 10 || c == 13 || c == 9 || c == 12) continue; else break;
}
return str.substring(i, j + 1);
}
function trim28(str) {
var c;
for (var i = 0, j = str.length; i < j; i++) {
c = str.charCodeAt(i);
if (c == 32 || c == 10 || c == 13 || c == 9 || c == 12) continue; else break;
}
for (j--; j >= i; j--) {
c = str.charCodeAt(j);
if (c == 32 || c == 10 || c == 13 || c == 9 || c == 12) continue; else break;
}
return str.substring(i, j + 1);
}
function trim_native(str) {
return str.trim(); // should throw is no such function
}
function trim19(str){
var str = str.replace(/^\s\s*/, ''),
ws = /\s/,
i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i + 1);
}
// Licensed under BSD - http://flesler.blogspot.com/2008/11/fast-trim-function-for-javascript.html
function myBestTrim( str ){
var start = -1,
end = str.length;
while( str.charCodeAt(--end) < 33 ){};
while( str.charCodeAt(++start) < 33 ){};
return str.substring( start, end + 1 );
};
var inp = " \n\n \n\n a \n a\n a\n a\n a \n\t\ta\t\ta\njahsdkjahkjshdakjhsdkahskd akhsjdh akjshd kashdkajhsd kajshd \nkauyiuqhwep iasldk qpwoie ad \n askdjaslkdjaslkjdaoiur qowioqwhr aspodiquw ijasod iqwiue pqowipoqiw epoqiwpeo iaslkjdqur \t kjahsdkj hakshd\nkajhdk\nk as d \t\n".split('\n');
function e(f) {
var x = [];
for (i = 0; i < inp.length; i++) {
x.push(f(inp[i]));
}
return x;
}
//it's unfair to wrap the native function, within an outer dummy function since this doesn't represent real world usage
function native(){
var x = [];
for (i = 0; i < inp.length; i++) {
x.push(inp[i].trim());
}
return x;
}
</script>
//Need to reset input after each time, otherwise subsequent tests have advantage cause the string doesn't have whitespace
inp = " \n\n \n\n a \n a\n a\n a\n a \n\t\ta\t\ta\njahsdkjahkjshdakjhsdkahskd akhsjdh akjshd kashdkajhsd kajshd \nkauyiuqhwep iasldk qpwoie ad \n askdjaslkdjaslkjdaoiur qowioqwhr aspodiquw ijasod iqwiue pqowipoqiw epoqiwpeo iaslkjdqur \t kjahsdkj hakshd\nkajhdk\nk as d \t\n".split('\n');
Ready to run.
Test | Ops/sec | |
---|---|---|
trim-native |
| ready |
trim27 |
| ready |
trim19 |
| ready |
trim28 |
| ready |
trim_yesudeep |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.