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
Porovnanie výkonu rôznych implementácií ELSCR
Info: http://php.vrana.cz/elscr-elegantne.php
<script>
var getRandom = (function () {
var chars = ['+', 'ě', 'ľ', 'š', 'č', 'ř', 'ť', 'ž', 'ý', 'á', 'í', 'é'];
return function() {
return chars[Math.floor(Math.random()*(chars.length+1))];
}
})();
var sampleArray = [];
for(var i=0; i<1000; i++){
sampleArray.push(getRandom());
}
var sampleStr = sampleArray.join('');
</script>
function elscr_jv1(value) {
var source = '+ůěľščřťžýáíé';
var target = '1122345567890';
var map = { };
for (var i = 0; i < source.length; i++) {
map[source[i]] = target[i];
}
var pattern = new RegExp('[' + source.replace(/[-\]^\\]/g, '\\$&') + ']', 'g');
return value.replace(pattern, function (match) {
return map[match];
});
}
var elscr_jv2 = (function () {
var source = '+ůěľščřťžýáíé';
var target = '1122345567890';
var map = { };
for (var i = 0; i < source.length; i++) {
map[source[i]] = target[i];
}
var pattern = new RegExp('[' + source.replace(/[-\]^\\]/g, '\\$&') + ']', 'g');
return function elscr(value) {
return value.replace(pattern, function (match) {
return map[match];
});
}
})();
function elscr_bonzi(value) {
var map = { '+': 1, 'ů': 1, 'ě': 2, 'ľ': 2, 'š': 3, 'č': 4, 'ř': 5, 'ť': 5, 'ž': 6, 'ý': 7, 'á': 8, 'í': 9, 'é': 0 };
var ret = '';
for (var i = 0; i < value.length; i++){
if(map[value[i]] !== undefined) ret += map[value[i]];
else ret += value[i];
}
return ret;
}
/**
* ELSCR plugin replaces czech and slovak characters +ěľščťžýáíé to numbers
*
* @author Jiri Knesl <jiri@sprintmasters.cz> http://www.knesl.com
* @licence BSD
* @link https://bitbucket.org/jiriknesl/lscr
*
*/
var ELSCR, module;
ELSCR = (function(){
ELSCR.displayName = 'ELSCR';
var prototype = ELSCR.prototype, constructor = ELSCR;
prototype.replaces = function(){
return {
"+": "1",
"ě": "2",
"ľ": "2",
"š": "3",
"č": "4",
"ř": "5",
"ť": "5",
"ž": "6",
"ý": "7",
"á": "8",
"í": "9",
"é": "0"
};
};
prototype.rewrite = function(value){
return this.rewriteWithReplaces(value, this.replaces());
};
prototype.rewriteWithReplaces = function(value, replaces){
var key, replacesWithoutFirst, replaceValue, valueReplaced;
key = this.firstKeyIn(replaces);
if (key === null) {
return value;
} else {
replacesWithoutFirst = replaces;
replaceValue = replaces[key] + "";
delete replacesWithoutFirst[key];
valueReplaced = this.replaceAllKeysInStringByValues(value, key, replaceValue);
return this.rewriteWithReplaces(valueReplaced, replacesWithoutFirst);
}
};
prototype.replaceAllKeysInStringByValues = function(value, key, replaceValue){
var index, replacedValue;
index = value.indexOf(key);
if (index !== -1) {
replacedValue = value.replace(key, replaceValue);
return this.replaceAllKeysInStringByValues(replacedValue, key, replaceValue);
} else {
return value;
}
};
prototype.firstKeyIn = function(replaces){
var firstKey, key;
firstKey = null;
for (key in replaces) {
if (replaces.hasOwnProperty(key)) {
firstKey = key;
break;
}
}
return firstKey;
};
function ELSCR(){}
return ELSCR;
}());
if (typeof module == 'undefined' || module === null) {
module = {};
}
module.exports = ELSCR;
function elscr_v6ak(value){
var map = { '+': 1, 'ů': 1, 'ě': 2, 'ľ': 2, 'š': 3, 'č': 4, 'ř': 5, 'ť': 5, 'ž': 6, 'ý': 7, 'á': 8, 'í': 9, 'é': 0 };
return value.replace(/./g, function(c){
return map[c] || c;
});
}
function Elscr() {
var map = {'+': 1, 'ů': 1, 'ě': 2, 'ľ': 2, 'š': 3, 'č': 4, 'ř': 5, 'ť': 5, 'ž': 6, 'ý': 7, 'á': 8, 'í': 9, 'é': 0};
var regExpChars = '';
for (var key in map) {
regExpChars += key;
}
var regExp = new RegExp('[' + regExpChars + ']', 'g');
this.replace = function(value) {
return value.replace(regExp, function(match){
return map[match];
});
};
}
var elscr_jv2_with_cached_fn = (function () {
var source = '+ůěľščřťžýáíé';
var target = '1122345567890';
var map = { };
for (var i = 0; i < source.length; i++) {
map[source[i]] = target[i];
}
var pattern = new RegExp('[' + source.replace(/[-\]^\\]/g, '\\$&') + ']', 'g');
function fn (match){ return map[match]; }
return function elscr(value) {
return value.replace(pattern, fn);
}
})();
Ready to run.
Test | Ops/sec | |
---|---|---|
Jakub Vrana #1 |
| ready |
Jakub Vrana #2 (closure) |
| ready |
Bonzi |
| ready |
Knesl |
| ready |
v6ak |
| ready |
Elscr |
| ready |
closure with cached fn |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.