4 ms·
Because the JSON grammar is much simpler than JavaScript’s grammar, JSON can be parsed more efficiently than JavaScript. Hmm.. shouldn't that hold for
by no_gravity 7y ago
Because the JSON grammar is much simpler than
JavaScript’s grammar, JSON can be parsed more
efficiently than JavaScript.
Hmm.. shouldn't that hold for most programming languages then?
Let's try it for PHP:
time php -r 'for ($i=0;$i<10000000; $i++) $data = [1,2,3];'
real 0m0,173s
user 0m0,161s
sys 0m0,012s
time php -r 'for ($i=0;$i<10000000; $i++) $data = json_decode("[1,2,3]");'
real 0m4,125s
user 0m4,120s
sys 0m0,005s
So for 10 million repetitions, a small PHP structure is about 20x faster then parsing JSON. But to test the point of the article, one should use the sama data structure it uses (https://raw.githubusercontent.com/WebKit/webkit/ffdd2799d3237993354978b9d0cdd1d248fe3787/PerformanceTests/JetStream2/SeaMonster/inspector-json-payload.js https://raw.githubusercontent.com/WebKit/webkit/ffdd2799d323...) and parse it only once.
- eloff 7y agoThis completely depends on the implementation of JSON in your language. That happens to be heavily optimized in JavaScript for obvious reasons.
- toast0 7y agotry json_decode(..., true) to get a similar data structure?
- alufers 7y agoI believe you are doing it wrong here, since the code is parsed only once and then executed in a loop in the first sample. If you parsed one array with 10000000 elements it should be faster.
- no_gravity 7y agoCorrect.
- elpocko 7y agoBut you're not parsing the array 10000000 times in the first example. Use `eval`.
- no_gravity 7y agoNot sure if eval is the right choice as the article compares parsing json to using a native js structure as well. The correct comparison would be to try it on the 7MB json data the article is based on.
- deleted 7y ago[deleted]
- bryanlarsen 7y ago$ time php -r 'for ($i=0;$i<10000000; $i++) $data = eval("[1,2,3];");' real 0m10.568s user 0m10.539s sys 0m0.028s
- sebringj 7y agoPHP doesn't run inside Chrome's JS engine as far as I know... probably apples and oranges here.
- pornel 7y agoYour example compares parsing 1 time with parsing 10000000 times. PHP does not re-parse code during execution.
- shortercode 7y agoThe possible syntax for Object literals is a lot more complicated than what JSON can contain. Hence the JSON parser is faster than the JS parser for the same object. JS object literals allow such fun things as: { "foo": "bar", foo1: function () { return "bar" }, foo2: () => "bar", foo3 () { return "bar" }, get foo4 () { return "bar" }, ["foo5"]: () => "bar" }
- pitaj 7y agoPlease don't quote with code blocks. For mobile users: > Because the JSON grammar is much simpler than JavaScript’s grammar, JSON can be parsed more efficiently than JavaScript.
- basilgohar 7y agoA lot of the overhead in there is the call to the function json_decode. Function calls are much more expensive than operators in PHP. So this really isn't apples-to-apples. The differences in data structures between the two languages may also play a significant role, what with PHP using complex z-val structs behind the scenes for almost all data types.
- bryanlarsen 7y agoBetter test: time for f in `seq 1000` ; do php -r '$data=[1,2,3];'; done real 0m11.169s user 0m6.765s sys 0m4.477s time for f in `seq 1000` ; do php -r '$data=json_decode("[1,2,3]");'; done real 0m9.997s user 0m6.093s sys 0m3.974s
- no_gravity 7y agoAwesome. So it does hold true for PHP as well.
- thayne 7y ago> shouldn't that hold for most programming languages then? Only for interpreted languages that have a native implementation of the JSON parser (that is the json parser isn't itself interpreted)