Skip to content

Instantly share code, notes, and snippets.

@summer-wu
Created May 23, 2023 06:48
Show Gist options
  • Save summer-wu/030bd287f8a973d66ff1aa71d7e07569 to your computer and use it in GitHub Desktop.
Save summer-wu/030bd287f8a973d66ff1aa71d7e07569 to your computer and use it in GitHub Desktop.
JSON.stringify() is 3x slower in jsc
function getRandomStringWithLength(l) {
let s = String(Math.random());
while (true) {
if (s.length >= l) {
return s.substring(0, l);
}
s += ':' + String(Math.random());
}
}
const obj = {};
for (let i = 0; i < 1000; i++) {
obj[i] = getRandomStringWithLength(1000);
}
//obj should contains around 1M bytes after JSON.stringify
const str_arr = [];
const t1 = Date.now();
for (let i = 0; i < 1000; i++) {
const s = JSON.stringify(obj);
str_arr.push(s);
}
if (globalThis.print) {
print(`Running JSON.stringify 1000 times cost ${Date.now() - t1}ms`);
} else {
console.log(`Running JSON.stringify 1000 times cost ${Date.now() - t1}ms`);
}
#!/bin/bash
hermes=/your_working_copy/ios/Pods/hermes-engine/destroot/bin/hermes
jsc=/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Helpers/jsc
echo "----hermes"
$hermes ./1.js
echo "----jsc"
$jsc ./1.js
echo "----node"
node ./1.js
$ bash bench.sh
----hermes
Running JSON.stringify 1000 times cost 7265ms
----jsc
Running JSON.stringify 1000 times cost 1612ms
----node
Running JSON.stringify 1000 times cost 2911ms
@summer-wu
Copy link
Author

$ uname -a
Darwin C02GL0FZMD6R 22.4.0 Darwin Kernel Version 22.4.0: Mon Mar 6 21:00:17 PST 2023; root:xnu-8796.101.5~3/RELEASE_X86_64 x86_64 i386 MacBookPro16,1 Darwin

$hermes --version
Hermes release version: 0.11.0

$ node --version
v16.15.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment