C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Parse: We invoke JSON.parse on the text string. This returns an array of 3 elements—the strings cat, bird and frog.
JavaScript program that uses JSON.parse
// This is a JSON text string.
var text = '["cat", "bird", "frog"]';
// Use JSON.parse to convert string to array.
var result = JSON.parse(text);
// Print result count.
console.log("COUNT: " + result.length);
// ... Loop over result.
for (var i = 0; i < result.length; i++) {
console.log("JSON PARSED ITEM: " + result[i]);
}
Output
COUNT: 3
JSON PARSED ITEM: cat
JSON PARSED ITEM: bird
JSON PARSED ITEM: frog
JavaScript program that uses JSON.stringify
var array = ["cat", "bird", "frog"];
// Use JSON.stringify to get a string.
var result = JSON.stringify(array);
console.log("STRINGIFY: " + result);
Output
STRINGIFY: ["cat","bird","frog"]
Version 1: Here we invoke JSON.parse on a string that has no whitespace (space characters).
Version 2: In this version of the code, we call JSON.parse on a string that has spaces.
Result: Each space must be processed by the JSON.parse method. So omitting spaces (minifying) will help performance.
JavaScript program that times JSON.parse, no whitespace
var x1 = performance.now();
// Version 1: parse JSON with no whitespace.
for (var i = 0; i < 1000000; i++) {
var result = JSON.parse('["cat","dog","bird"]');
}
var x2 = performance.now();
// Version 2: parse JSON with whitespace.
for (var i = 0; i < 1000000; i++) {
var result = JSON.parse('["cat", "dog", "bird"]');
}
var x3 = performance.now();
// Results.
console.log("TIME 1: " + (x2 - x1));
console.log("TIME 2: " + (x3 - x2));
Output
TIME 1 (2019): 542.299
TIME 2: 528.330
Version 1: The array of 3 string elements in stored in a string and then expanded with JSON.parse in each iteration.
Version 2: The array is specified directly in the JavaScript program. It is only parsed once at load time.
Result: It is far faster to place a JavaScript object directly in the page source instead of in a JSON string. This reduces parsing time.
JavaScript program that benchmarks JSON.parse
var count = 0;
var x1 = performance.now();
// Version 1: parse the JSON array on each iteration.
for (var i = 0; i < 1000000; i++) {
var text = '["cat", "bird", "frog"]';
var result = JSON.parse(text);
count += result.length;
}
var x2 = performance.now();
// Version 2: use a JavaScript array.
for (var i = 0; i < 1000000; i++) {
var result = ["cat", "bird", "frog"];
count += result.length;
}
var x3 = performance.now();
// Results.
console.log("TIME 1: " + (x2 - x1));
console.log("TIME 2: " + (x3 - x2));
Output
TIME 1 (2019): 541.139
TIME 2: 10.509
In 2019: Having a string literal with JSON in it, and then calling JSON.parse, is faster than having an object literal in raw JavaScript.
Cost of JavaScript: v8.devSo: For large object literals, keep them at the top-level (not in a nested function) and use JSON.parse on a string literal.
Tip: This can reduce total parsing time significantly—it helps the most on the largest object literals.