Skip to content

Instantly share code, notes, and snippets.

@llimacruz
Created June 29, 2019 11:04
Show Gist options
  • Save llimacruz/1bf947c0a6be11a9e673736e5593288a to your computer and use it in GitHub Desktop.
Save llimacruz/1bf947c0a6be11a9e673736e5593288a to your computer and use it in GitHub Desktop.
Comparar a criação de um objeto literal com um parse de json
const runTest = () => {
const n = 10000000;
// literal
let init = new Date();
for (let i = 1; i <= n; i++) {
const data = { foo: 42, bar: 1337 };
}
let end = new Date();
console.log('literal: ' + (end - init));
// parsed
init = new Date();
for (let i = 1; i <= n; i++) {
const dataP = JSON.parse('{ "foo": 42, "bar": 1337 }')
}
end = new Date();
console.log('parsed: ' + (end - init));
}
for (let i = 1; i <= 10; i++) {
runTest();
}
@llimacruz
Copy link
Author

Blog que originou o teste: https://v8.dev/blog/cost-of-javascript-2019#json
Resultado do código acima executado num i7 com Ubuntu, mostra para o exemplo dado no artigo, object literal é muito mais rápido.
literal: 78
parsed: 4847
literal: 74
parsed: 4876
literal: 78
parsed: 4723
literal: 79
parsed: 4749
literal: 83
parsed: 4873
literal: 84
parsed: 4775
literal: 84
parsed: 4776
literal: 85
parsed: 4748
literal: 84
parsed: 4730
literal: 86
parsed: 4703

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