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
https://stackoverflow.com/questions/55651936/why-i-cant-use-string-charcodeat-at-elements-of-reduce-callback/55651977#55651977
Convolution of the user's nickname into one of the default avatars filename.
For Habr with ❤️
(function(){"use strict";
var U = 0; // un_eliminate
var k = 0;
var users = [
{
id: 0,
alias: "User_Nick_Name_00"
},{
id: 1,
alias: "User_01"
}
];
function generateDefAvatarNum(alias){
var sum = 0;
var len = alias.length;
for(var i = 0; i < len; i++){
sum += alias.charCodeAt(i); // `charCodeAt(i)` faster with i++, slower with i--
}
return (""+(Math.abs(sum%200)+1)).padStart(3,"0");
};
var getDefAvatarFileNum = (function(){
var respondentId = users[1].id;
// cache
var respondentDefAvatarNum = "",
myDefAvatarNum = "";
// or `["",""]` - code below is faster than with `new Array(2)` or `[,,]`
return function(author){
var id = author.id;
var defAvatarNum = (id === respondentId ? respondentDefAvatarNum : myDefAvatarNum);
if(!defAvatarNum){
defAvatarNum = generateDefAvatarNum(author.alias);
if(id === respondentId) respondentDefAvatarNum = defAvatarNum;
else myDefAvatarNum = defAvatarNum;
}
return defAvatarNum;
};
})();
window.__un_eliminate__ = U;
})(); // (function(){"use strict";
Ready to run.
Test | Ops/sec | |
---|---|---|
Original: .split().reduce() → charCodeAt(0) |
| ready |
for … of → charCodeAt(0) |
| ready |
for → charCodeAt(i) |
| ready |
Cache: for → charCodeAt(i) |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.