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
JSON.stringify (if avail) vs custom implementations
var shortJSON = {
"fathers|5-10" : [
{
"married|0-1" : true,
"name" : "@MALE_FIRST_NAME @LAST_NAME",
"sons" : null,
"daughters|0-3" : [
{
"age|0-31" : 0,
"name" : "@FEMALE_FIRST_NAME"
}
]
}
]
};
var longJSON = {
"fathers" : [
{
"married" : false,
"name" : "William Martinez",
"sons" : null,
"daughters" : [
{
"age" : 8,
"name" : "Michelle"
},
{
"age" : 29,
"name" : "Donna"
}
]
},
{
"married" : false,
"name" : "Thomas Taylor",
"sons" : null,
"daughters" : [
{
"age" : 6,
"name" : "Angela"
}
]
},
{
"married" : false,
"name" : "Christopher Johnson",
"sons" : null,
"daughters" : [
{
"age" : 11,
"name" : "Laura"
},
{
"age" : 27,
"name" : "Shirley"
}
]
},
{
"married" : true,
"name" : "Christopher Davis",
"sons" : null,
"daughters" : [
{
"age" : 17,
"name" : "Carol"
},
{
"age" : 28,
"name" : "Michelle"
}
]
},
{
"married" : false,
"name" : "Christopher Jackson",
"sons" : null,
"daughters" : [
{
"age" : 4,
"name" : "Linda"
},
{
"age" : 29,
"name" : "Betty"
}
]
},
{
"married" : false,
"name" : "Michael Harris",
"sons" : null,
"daughters" : [
{
"age" : 8,
"name" : "Carol"
},
{
"age" : 26,
"name" : "Deborah"
},
{
"age" : 14,
"name" : "Sharon"
}
]
}
]
};
var customStringify1 = function stringify(o){
var type = typeof o, tmp, k;
var objectToString = Object.prototype.toString;
if (type === 'string') {
return '"'+o+'"';
}
else if (type === 'number') {
return o;
}
else if (type === 'object') {
tmp = objectToString.call(o);
if(tmp ==='[object Object]') {
tmp = '{';
for (k in o) {
if(o.hasOwnProperty(k))
tmp+= '"'+k+'":'+stringify(o[k])+",";
}
tmp = tmp.slice(0,-1)+"}";
return tmp;
}
else if(tmp ==='[object Array]') {
k = o.length;
while (k--) {
o[k] = stringify(o[k]);
}
return '['+o.join(',')+']';
}
else {
return String(o);
}
}
};
var customStringify2 = function (obj) {
var t = typeof (obj);
if (t != "object" || obj === null) {
// simple data type
if (t == "string") obj = '"'+obj+'"';
return String(obj);
}
else {
// recurse array or object
var n, v, json = [], arr = (obj && obj.constructor == Array);
for (n in obj) {
v = obj[n]; t = typeof(v);
if (t == "string") v = '"'+v+'"';
else if (t == "object" && v !== null) v = JSON.stringify(v);
json.push((arr ? "" : '"' + n + '":') + String(v));
}
return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
}
};
Ready to run.
Test | Ops/sec | |
---|---|---|
JSON.stringify (internal) |
| ready |
customStringify1 |
| ready |
customStringify2 |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.