fnando / i18n-js

It's a small library to provide the I18n translations on the Javascript. It comes with Rails support.
MIT License
3.77k stars 520 forks source link

JSON.parse approach is much faster than object literal #605

Closed pastak closed 3 years ago

pastak commented 3 years ago

JSON.parse is much faster than object literal

Because the JSON grammar is much simpler than JS's object literal so make it faster about x1.5 on some browser's JS engines, not only V8 with Chrome but also JavaScriptCore with Safari or Chakra with Internet Explorer

cf. https://v8.dev/blog/cost-of-javascript-2019#json

with pretty_print option

Pretty-printed JSON should be contained into template string to pass JSON.parse but Internet Explorer 11 doesn't support template string so it avoid using JSON.parse when pretty_print option is true.

PikachuEXE commented 3 years ago

Thanks for providing link to the article As the tests are passing I will just release this soon and let people test it (maybe they won't notice a thing)

pastak commented 3 years ago

@PikachuEXE Oh, it maybe happens problem with JSON contains '...

PikachuEXE commented 3 years ago

Try to create a failing case We also have an option called json_only (not well documented) Introduced in https://github.com/fnando/i18n-js/pull/524 I will hold my release until you figure if single quotes would cause issue after this PR merged

pastak commented 3 years ago

I have become aware json_only and it keeps the same results as before 👍