Skip to content

Instantly share code, notes, and snippets.

@SmolinPavel
Created March 27, 2019 07:56
Show Gist options
  • Save SmolinPavel/65be8d446916516a248785a4dd11adc5 to your computer and use it in GitHub Desktop.
Save SmolinPavel/65be8d446916516a248785a4dd11adc5 to your computer and use it in GitHub Desktop.
Measuring performance (spead vs Array.forEach)
<html>
<head>
<title>JS Sandbox</title>
</head>
<body>
<script>
const object = {
test: "test",
paul: "paul",
morning: { test: "test", paul: "paul" }
};
let test1 = { test: "default" };
const t0 = performance.now();
Object.keys(object).forEach(item => {
test1[item] = object[item];
});
const t1 = performance.now();
console.log("Call to forEach took " + (t1 - t0) + " milliseconds.");
let test2 = { test: "default" };
const t2 = performance.now();
test2 = { ...test2, ...object };
const t3 = performance.now();
console.log("Call to spread took " + (t3 - t2) + " milliseconds.");
</script>
</body>
</html>
@SmolinPavel
Copy link
Author

Spread seems to be faster in my checks:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment