Skip to content

Instantly share code, notes, and snippets.

@g-k
Created April 10, 2017 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g-k/c1efc1527cd74305b1e3cd272a49453a to your computer and use it in GitHub Desktop.
Save g-k/c1efc1527cd74305b1e3cd272a49453a to your computer and use it in GitHub Desktop.
escape json rough perf tests
~/hoek - [add-escape-json●] » node --version
v7.1.0
~/hoek - [add-escape-json●] » uname -a
Darwin gguthe-23818 15.6.0 Darwin Kernel Version 15.6.0: Mon Jan 9 23:07:29 PST 2017; root:xnu-3248.60.11.2.1~1/RELEASE_X86_64 x86_64
~/hoek - [add-escape-json●] » cat test/json_escape_many_replace_calls.js
function escapeJson(input) {
if (!input) {
return '';
}
return input.replace(/</g, '\\u003c')
.replace(/>/g, '\\u003e')
.replace(/&/g, '\\u0026')
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029');
}
const testJson = JSON.stringify(require(process.argv[1]));
for (var i = 0; i < 1000000; i++) {
escapeJson(testJson);
}
~/hoek - [add-escape-json●] » cat test/json_escape_elif.js
function escapeJson(input) {
if (!input) {
return '';
}
return input.replace(/[<>&\u2028\u2029]/g, (match) => {
if (match === '<') {
return '\\u003c';
} else if (match === '>') {
return '\\u003e';
} else if (match === '&') {
return '\\u0026';
} else if (match === '\u2028') {
return '\\u2028';
} else {
return '\\u2029';
};
});
}
const testJson = JSON.stringify(require(process.argv[1]));
for (var i = 0; i < 1000000; i++) {
escapeJson(testJson);
}
~/hoek - [add-escape-json●] » cat test/json_escape_switch.js
function escapeJson(input) {
if (!input) {
return '';
}
return input.replace(/[<>&\u2028\u2029]/g, (match) => {
switch (match) {
case '<':
return '\\u003c';
case '>':
return '\\u003e';
case '&':
return '\\u0026';
case '\u2028':
return '\\u2028';
default:
return '\\u2029';
};
});
}
const testJson = JSON.stringify(require(process.argv[1]));
for (var i = 0; i < 1000000; i++) {
escapeJson(testJson);
}
~/hoek - [add-escape-json●] » # wget https://raw.githubusercontent.com/miloyip/nativejson-benchmark/master/data/twitter.json
~/hoek - [add-escape-json●] » node --trace-deopt test/json_escape_switch.js twitter.json
~/hoek - [add-escape-json●] » node --trace-deopt test/json_escape_many_replace_calls.js twitter.json
~/hoek - [add-escape-json●] » node --trace-deopt test/json_escape_elif.js twitter.json
~/hoek - [add-escape-json●] » # none hit deoptimizations
~/hoek - [add-escape-json●] » for X in $(seq 15); do time node test/json_escape_switch.js twitter.json; done
node test/json_escape_switch.js twitter.json 0.22s user 0.01s system 99% cpu 0.236 total
node test/json_escape_switch.js twitter.json 0.21s user 0.01s system 99% cpu 0.224 total
node test/json_escape_switch.js twitter.json 0.21s user 0.01s system 99% cpu 0.220 total
node test/json_escape_switch.js twitter.json 0.22s user 0.01s system 98% cpu 0.237 total
node test/json_escape_switch.js twitter.json 0.22s user 0.01s system 99% cpu 0.230 total
node test/json_escape_switch.js twitter.json 0.21s user 0.01s system 100% cpu 0.219 total
node test/json_escape_switch.js twitter.json 0.21s user 0.01s system 98% cpu 0.224 total
node test/json_escape_switch.js twitter.json 0.21s user 0.01s system 100% cpu 0.218 total
node test/json_escape_switch.js twitter.json 0.21s user 0.01s system 99% cpu 0.222 total
node test/json_escape_switch.js twitter.json 0.21s user 0.01s system 99% cpu 0.217 total
node test/json_escape_switch.js twitter.json 0.21s user 0.01s system 100% cpu 0.219 total
node test/json_escape_switch.js twitter.json 0.22s user 0.01s system 100% cpu 0.231 total
node test/json_escape_switch.js twitter.json 0.23s user 0.01s system 100% cpu 0.238 total
node test/json_escape_switch.js twitter.json 0.22s user 0.01s system 99% cpu 0.229 total
node test/json_escape_switch.js twitter.json 0.21s user 0.01s system 99% cpu 0.222 total
~/hoek - [add-escape-json●] » for X in $(seq 15); do time node test/json_escape_elif.js twitter.json; done
node test/json_escape_elif.js twitter.json 0.22s user 0.01s system 98% cpu 0.235 total
node test/json_escape_elif.js twitter.json 0.21s user 0.01s system 99% cpu 0.225 total
node test/json_escape_elif.js twitter.json 0.22s user 0.01s system 99% cpu 0.235 total
node test/json_escape_elif.js twitter.json 0.22s user 0.01s system 99% cpu 0.231 total
node test/json_escape_elif.js twitter.json 0.22s user 0.01s system 99% cpu 0.233 total
node test/json_escape_elif.js twitter.json 0.23s user 0.01s system 95% cpu 0.257 total
node test/json_escape_elif.js twitter.json 0.23s user 0.01s system 98% cpu 0.252 total
node test/json_escape_elif.js twitter.json 0.22s user 0.01s system 98% cpu 0.235 total
node test/json_escape_elif.js twitter.json 0.22s user 0.01s system 98% cpu 0.237 total
node test/json_escape_elif.js twitter.json 0.22s user 0.01s system 99% cpu 0.236 total
node test/json_escape_elif.js twitter.json 0.22s user 0.01s system 99% cpu 0.231 total
node test/json_escape_elif.js twitter.json 0.22s user 0.01s system 99% cpu 0.230 total
node test/json_escape_elif.js twitter.json 0.21s user 0.01s system 99% cpu 0.225 total
node test/json_escape_elif.js twitter.json 0.21s user 0.01s system 99% cpu 0.226 total
node test/json_escape_elif.js twitter.json 0.22s user 0.01s system 100% cpu 0.231 total
~/hoek - [add-escape-json●] » for X in $(seq 15); do time node test/json_escape_many_replace_calls.js twitter.json; done
node test/json_escape_many_replace_calls.js twitter.json 0.62s user 0.01s system 99% cpu 0.636 total
node test/json_escape_many_replace_calls.js twitter.json 0.62s user 0.01s system 99% cpu 0.637 total
node test/json_escape_many_replace_calls.js twitter.json 0.64s user 0.01s system 99% cpu 0.652 total
node test/json_escape_many_replace_calls.js twitter.json 0.62s user 0.01s system 99% cpu 0.631 total
node test/json_escape_many_replace_calls.js twitter.json 0.62s user 0.01s system 99% cpu 0.630 total
node test/json_escape_many_replace_calls.js twitter.json 0.63s user 0.01s system 99% cpu 0.643 total
node test/json_escape_many_replace_calls.js twitter.json 0.62s user 0.01s system 99% cpu 0.631 total
node test/json_escape_many_replace_calls.js twitter.json 0.65s user 0.01s system 98% cpu 0.671 total
node test/json_escape_many_replace_calls.js twitter.json 0.62s user 0.01s system 99% cpu 0.636 total
node test/json_escape_many_replace_calls.js twitter.json 0.67s user 0.01s system 98% cpu 0.689 total
node test/json_escape_many_replace_calls.js twitter.json 0.62s user 0.01s system 99% cpu 0.636 total
node test/json_escape_many_replace_calls.js twitter.json 0.62s user 0.01s system 99% cpu 0.630 total
node test/json_escape_many_replace_calls.js twitter.json 0.63s user 0.01s system 99% cpu 0.646 total
node test/json_escape_many_replace_calls.js twitter.json 0.63s user 0.01s system 99% cpu 0.641 total
node test/json_escape_many_replace_calls.js twitter.json 0.68s user 0.01s system 98% cpu 0.705 total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment