var json = JSON.stringify({
"type": "l2update",
"product_id": "BTC-USD",
"time": "2019-08-14T20:42:27.265Z",
"changes": [
[
"buy",
"10101.80000000",
"0.162567"
]
]
});
var o = JSON.parse(json);
if (o.type === 'l2update') {
// do something cool
}
if (json.indexOf('l2update') !== -1) {
// do something cool
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
JSON.parse | |
String.indexOf |
Test name | Executions per second |
---|---|
JSON.parse | 997291.6 Ops/sec |
String.indexOf | 12262335.0 Ops/sec |
Benchmark Overview
The provided benchmark measures the performance difference between two approaches to check if a specific string exists in another string: JSON.parse
and string.indexOf
.
Options Being Compared
'l2update'
exists in the original JSON string.Pros and Cons of Each Approach
string.indexOf
.JSON.parse
, making it suitable for applications with limited resources.Library Used
The benchmark uses the built-in JSON
object in JavaScript, which provides methods for parsing and manipulating JSON data. The JSON.stringify()
method is used to convert a JavaScript object into a string, while JSON.parse()
is used to parse a JSON string back into an object.
Special JS Feature or Syntax
None mentioned in this benchmark.
Other Considerations
The benchmark assumes that the input JSON string contains only ASCII characters. If the string can contain non-ASCII characters, additional considerations may be necessary.
Alternatives
Other alternatives for checking if a substring exists within another string include:
It's worth noting that the benchmark results will depend on various factors, such as the specific hardware, software, and environment used for testing.