var testString = '{ "type": "Feature", "properties": { "OBJECTID": 1, "OBJECTID_12": 1, "ELEV_IN_FT": 5397, "DATE_CREATED": "318816000000", "DATE_EDITED": "", "name": "Agua Sal Creek", "type": "Stream", "source": "USGS_PlaceNamesUSA", "description": "", "state": "Arizona", "class": "Place Name", "map_layer": "Place Name", "state_ab": "AZ", "lat": 36.461112200202791, "long": -109.47843940044683, "gmu_id": "", "hasname": "" }, "geometry": { "type": "Point", "coordinates": [ -109.478439400446817, 36.461112200202805, 0.0 ] } },\n{ "type": "Feature", "properties": { "OBJECTID": 1, "OBJECTID_12": 1, "ELEV_IN_FT": 5397, "DATE_CREATED": "318816000000", "DATE_EDITED": "", "name": "Agua Sal Creek", "type": "Stream", "source": "USGS_PlaceNamesUSA", "description": "", "state": "Arizona", "class": "Place Name", "map_layer": "Place Name", "state_ab": "AZ", "lat": 36.461112200202791, "long": -109.47843940044683, "gmu_id": "", "hasname": "" }, "geometry": { "type": "Point", "coordinates": [ -109.478439400446817, 36.461112200202805, 0.0 ] } },\n{ "type": "Feature", "properties": { "OBJECTID": 1, "OBJECTID_12": 1, "ELEV_IN_FT": 5397, "DATE_CREATED": "318816000000", "DATE_EDITED": "", "name": "Agua Sal Creek", "type": "Stream", "source": "USGS_PlaceNamesUSA", "description": "", "state": "Arizona", "class": "Place Name", "map_layer": "Place Name", "state_ab": "AZ", "lat": 36.461112200202791, "long": -109.47843940044683, "gmu_id": "", "hasname": "" }, "geometry": { "type": "Point", "coordinates": [ -109.478439400446817, 36.461112200202805, 0.0 ] } },\n{ "type": "Feature", "properties": { "OBJECTID": 1, "OBJECTID_12": 1, "ELEV_IN_FT": 5397, "DATE_CREATED": "318816000000", "DATE_EDITED": "", "name": "Agua Sal Creek", "type": "Stream", "source": "USGS_PlaceNamesUSA", "description": "", "state": "Arizona", "class": "Place Name", "map_layer": "Place Name", "state_ab": "AZ", "lat": 36.461112200202791, "long": -109.47843940044683, "gmu_id": "", "hasname": "" }, "geometry": { "type": "Point", "coordinates": [ -109.478439400446817, 36.461112200202805, 0.0 ] } },\n'
var values = testString.split("\n");
var value1 = values[0];
var value2 = values[1];
var value3 = values[2];
var regex = /.+((?=\n)|$)/g
var value1 = regex[0];
var value2 = regex[1];
var value3 = regex[2];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Splitting | |
Regex |
Test name | Executions per second |
---|---|
Splitting | 20250144.0 Ops/sec |
Regex | 37907584.0 Ops/sec |
The provided benchmark compares two approaches for parsing and extracting data from a string representation of JSON objects in JavaScript: String Splitting and Regular Expression (Regex) Matching.
testString
that is a multiline string of JSON-like data, where each object represents a geographic feature.Splitting Approach
split
method to divide the string on newline characters (\n
), creating an array of individual JSON strings. The benchmark retrieves the first three elements of this array.var values = testString.split("\n");
var value1 = values[0];
var value2 = values[1];
var value3 = values[2];
Regular Expression Approach
var regex = /.+((?=\n)|$)/g;
var value1 = regex[0];
var value2 = regex[1];
var value3 = regex[2];
The results of the benchmarks show the performance in terms of executions per second:
From these results, we see that the Regex approach performed significantly better in this specific case, which may be influenced by the way regex is optimized for pattern matching in JavaScript engines.
When choosing between these two methods, a developer should consider factors such as:
Apart from the provided methods, other techniques could include:
testString
were in valid JSON format, you could convert it into JavaScript objects using JSON.parse()
, but this wouldn’t directly apply to the way the data is structured in this benchmark.lodash
or utility libraries could provide additional string manipulation functionalities, but may add overhead and complexity.In summary, opting for either method depends on specific needs: quick, predictable performance versus flexible, more robust input handling.