Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Stringify vs Assign
(version: 0)
Comparing performance of:
Stringify vs Object.assign
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { "crossKey": "EURUSD", "dayLowPrice": 1.13045, "dayHighPrice": 1.13535, "previousClosePrice": 1.13215, "priceHist1": 107190404, "priceHist2": 250587665, "priceHist3": 765906410, "priceHist4": 274925633, "sparkline": 102925375, "spotPrice": 1.1307, "midPrice": 1.1307, "precision": 4, "percentChanged": -0.128, "threeMonthForecast": 1.08, "sixMonthForecast": 1.04, "tradable": true, "deliverable": true, "key": "EURUSD", "displayName": "EUR USD", "symbolId": "EURUSD", "objectId": 1, "assetType": "fx", "percentChange": -0.13, "selected": true, "lineChart": "9991581684158672254519078266440407", "barChart": [9, 43, 92, 47, 41, 21, 75, 32, 51, 19, 56, 97, 89, 36, 70, 2, 83, 61, 88, 47, 31, 54], "candleChart": "3869460804391469604270058742695196", "midPriceModel": "Price: key EURUSD, rate 1.1307, precision 4, appendFrac false, => 1.13 07 ", "spotPriceModel": "Price: key EURUSD, rate 1.1307, precision 4, appendFrac false, => 1.13 07 ", "crossCurrencies": ["EUR", "USD"], "unpackedSparkLine": { "price": 2, "priceDirection": 1, "fiveMinHigh": 4, "fiveMinLow": 0, "spike": 0, "scale": 30 }, "priceHistory": [3, 6, 7, 6, 4, 4, 7, 14, 31, 10, 16, 17, 22, 26, 13, 19, 15, 10, 8, 6, 6, 2, 2, 1] };
Tests:
Stringify
console.log(JSON.parse(JSON.stringify(obj)));
Object.assign
Object.assign({}, obj)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Stringify
Object.assign
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down what's being tested in the provided JSON, explain the options compared, and discuss their pros and cons. **Benchmark Definition** The benchmark definition is comparing two approaches to create a copy of an object: `JSON.parse(JSON.stringify(obj))` (Stringify) and `Object.assign({}, obj)` (Object.assign). **Options Compared** 1. **Stringify**: This approach uses the `JSON.parse()` function to convert the JSON string representation of the original object (`obj`) back into a JavaScript object, which is then assigned to a new variable. The resulting object is a deep copy of the original object. 2. **Object.assign()**: This approach uses the `Object.assign()` method to create a shallow copy of the original object (`obj`). The object being copied must have all its enumerable own properties (i.e., its keys) directly accessible. **Pros and Cons** 1. **Stringify**: * Pros: It creates a deep copy of the original object, which can be useful for complex objects with nested structures. * Cons: It can be slower than `Object.assign()` because it involves parsing and serializing the JSON string representation of the object. 2. **Object.assign()**: * Pros: It is generally faster than `Stringify` because it only needs to iterate over the enumerable own properties of the original object, without needing to parse or serialize the data. * Cons: If the original object has nested structures that contain other objects, `Object.assign()` will only create a shallow copy of those inner objects. For deep copies, you need to use `JSON.parse(JSON.stringify(obj))`. **Library and Special JS Features** In this benchmark, neither library nor special JS features are explicitly mentioned. **Alternatives** If you want to compare the performance of different methods for creating object copies, here are some alternatives: * **Spread operator (`{...obj}`)**: This method is similar to `Object.assign()`, but it's a more concise and modern way to create a shallow copy. * **`JSON.parse(JSON.stringify(obj))` with custom replacer**: If you need to control which properties are copied, you can use the `replacer` option of the `JSON.stringify()` function. This method is similar to `Object.assign()`, but it provides more flexibility. Keep in mind that the choice of method for creating object copies ultimately depends on your specific use case and requirements.
Related benchmarks:
Angular 1.7.5 Copy vs Lodash 4.17.11 Clone Deep
JavaScript spread operator vs Object.assign vs swc performance vs set
JavaScript spread operator vs Object.assign vs swc performance vs structuredClone
JavaScript spread operator vs Object.assign vs swc performance vs structuredClone vs object.keys
Comments
Confirm delete:
Do you really want to delete benchmark?