Created
June 29, 2019 11:04
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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