var obj = {
description: 'Creates a deep copy of source, which should be an object or an array.',
myNumber: 123456789,
myBoolean: true,
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...'
}
};
var jsonString = '{"description":"Creates a deep copy of source, which should be an object or an array.","myNumber":123456789,"myBoolean":true,"jayson":{"stringify":"JSON.stringify() method converts a JavaScript value to a JSON string....","parse":"JSON.parse() method parses a JSON string..."}}'
JSON.parse(jsonString);
JSON.stringify(obj)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
JSON.parse | |
JSON.stringify |
Test name | Executions per second |
---|---|
JSON.parse | 2305754.2 Ops/sec |
JSON.stringify | 2993675.2 Ops/sec |
Overview of the Benchmark
The MeasureThat.net website allows users to create and run JavaScript microbenchmarks, and this specific benchmark compares the performance of two common JavaScript functions: JSON.stringify
and JSON.parse
.
Tested Options
The test cases compare the execution time of:
Pros and Cons of Each Approach
Library Used
In this benchmark, no specific JavaScript library is used. However, JSON
is a built-in JavaScript object that provides the necessary functions for working with JSON data.
Special JS Feature or Syntax
This benchmark does not use any special JavaScript features or syntax. It only relies on standard JavaScript functionality.
Alternative Approaches
If you were to write this benchmark from scratch, alternative approaches might include:
serialize.js
or YAML
) instead of JSON.JSON.parse
, such as using a library like json-stringify-safe
.Benchmark Preparation Code
The provided code creates an object obj
with some properties and initializes two variables: jsonString
(a JSON string representation of the object) and a new object with stringify
and parse
properties. The script preparation code is:
var obj = {
description: 'Creates a deep copy of source, which should be an object or an array.',
myNumber: 123456789,
myBoolean: true,
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...'.
}
};
var jsonString = '{\"description\":\"Creates a deep copy of source, which should be an object or an array.\",\"myNumber\":123456789,\"myBoolean\":true,"' +
'"jayson\":{\"stringify":"JSON.stringify() method converts a JavaScript value to a JSON string....","parse":"JSON.parse() method parses a JSON string..."}}';