<!--your preparation HTML code goes here-->
/*your preparation JavaScript code goes here
To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/
async function globalMeasureThatScriptPrepareFunction() {
// This function is optional, feel free to remove it.
// await someThing();
}
const bigObject = {
// primitives
key1: 42,
key2: "hello",
key3: true,
key4: null,
key5: undefined,
key6: 3.14,
// nested objects
key7: { a: 1, b: 2 },
key8: { nested: { deeper: "value" } },
key9: { x: "foo", y: "bar" },
// arrays
key10: [1, 2, 3],
key11: ["a", "b", "c"],
key12: [true, false, true],
// functions (methods)
key13: function () { return "method13"; },
key14: () => "method14",
// more primitives and mixtures
key15: "world",
key16: 0,
key17: false,
key18: "sample",
key19: 100,
key20: [4, 5, 6],
// additional nested objects
key21: { name: "Alice", age: 30 },
key22: { x: [1,2], y: { z: "end" } },
key23: { flag: true },
// more arrays
key24: ["x", "y", "z"],
key25: [7, 8, 9],
// more functions
key26: function () { return this.key2; },
key27: () => 27,
// additional primitive values
key28: "test",
key29: 999,
key30: false
};
const a = bigObject.key1;
const b = bigObject.key2;
const c = bigObject.key3;
const d = bigObject.key4;
const e = bigObject.key5;
const f = bigObject.key6;
const g = bigObject.key7;
const h = bigObject.key8;
const i = bigObject.key9;
const j = bigObject.key10;
const k = bigObject.key11;
const l = bigObject.key12;
const m = bigObject.key13;
const n = bigObject.key14;
const o = bigObject.key15;
const p = bigObject.key16;
const q = bigObject.key17;
const r = bigObject.key18;
const s = bigObject.key19;
const t = bigObject.key20;
const u = bigObject.key21;
const v = bigObject.key22;
const w = bigObject.key23;
const x = bigObject.key24;
const y = bigObject.key25;
const z = bigObject.key26;
const aa = bigObject.key27;
const bb = bigObject.key28;
const cc = bigObject.key29;
const dd = bigObject.key30;
const bigObject = {
// primitives
key1: 42,
key2: "hello",
key3: true,
key4: null,
key5: undefined,
key6: 3.14,
// nested objects
key7: { a: 1, b: 2 },
key8: { nested: { deeper: "value" } },
key9: { x: "foo", y: "bar" },
// arrays
key10: [1, 2, 3],
key11: ["a", "b", "c"],
key12: [true, false, true],
// functions (methods)
key13: function () { return "method13"; },
key14: () => "method14",
// more primitives and mixtures
key15: "world",
key16: 0,
key17: false,
key18: "sample",
key19: 100,
key20: [4, 5, 6],
// additional nested objects
key21: { name: "Alice", age: 30 },
key22: { x: [1,2], y: { z: "end" } },
key23: { flag: true },
// more arrays
key24: ["x", "y", "z"],
key25: [7, 8, 9],
// more functions
key26: function () { return this.key2; },
key27: () => 27,
// additional primitive values
key28: "test",
key29: 999,
key30: false
};
const {
key1, key2, key3, key4, key5, key6,
key7, key8, key9, key10, key11, key12,
key13, key14, key15, key16, key17, key18,
key19, key20, key21, key22, key23, key24,
key25, key26, key27, key28, key29, key30
} = bigObject;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
direct access | |
destructuringTest |
Test name | Executions per second |
---|---|
direct access | 12369508.0 Ops/sec |
destructuringTest | 12338572.0 Ops/sec |
The benchmark tests included in the provided JSON focus on evaluating the performance of two different methods for accessing properties from a JavaScript object: direct access and destructuring assignment. Both approaches are used to retrieve values from a complex JavaScript object (bigObject
) that contains a mixture of primitive values, nested objects, arrays, and functions.
Direct Access
const a = bigObject.key1;
const b = bigObject.key2;
const c = bigObject.key3;
// ... and so on for all keys in bigObject
Destructuring Assignment
const { key1, key2, key3, key4, key5, key6, key7, ... } = bigObject;
Performance Insight: Based on the benchmark results, destructuring assignment performed slightly better than direct property access, with 51,067,292
executions per second for destructuring compared to 50,671,376
for direct access. This indicates that destructuring may have a small efficiency gain in terms of execution speed, especially when dealing with a larger number of properties.
Browser and Device Platform: Both benchmarks were executed in Chrome 136 on a Mac OS X 10.15.7 device, indicating that performance can be browser-dependent. Different engines (like V8 for Chrome) may optimize destructuring differently compared to other engines (like SpiderMonkey for Firefox).
Object.keys() / Object.values():
Object.keys()
or Object.values()
to retrieve the keys or values of the object as arrays and then access them. While flexible, this method may introduce additional overhead since it creates arrays from the object properties.Manually Creating Functions:
Spread Syntax:
...
) in certain contexts to pull properties from objects, but it is typically used to merge or clone objects rather than to simply access them.In summary, this benchmark clearly illustrates the differences in performance and readability between direct access and destructuring in JavaScript. Ultimately, the choice between these methods may depend on the specific use case, project standards, and performance considerations in the broader context of the application.