Skip to content

Instantly share code, notes, and snippets.

@melikhov-dev
Last active April 11, 2023 10:04
Show Gist options
  • Save melikhov-dev/51f92e84b7aa9456a4cdba0550130df7 to your computer and use it in GitHub Desktop.
Save melikhov-dev/51f92e84b7aa9456a4cdba0550130df7 to your computer and use it in GitHub Desktop.
const N = 10000;
let prev1, prev2, prev3, prev4;
let counter1 = 0, counter2 = 0, counter3 = 0, counter4 = 0;
function test(obj) {
if (!prev1) {
prev1 = obj;
}
if (!%HaveSameMap(prev1, obj)) {
counter1++;
}
let result = 0;
for (let i = 0; i < N; i++) {
result += obj.a + obj.b
}
return result;
}
function test2(obj) {
if (!prev2) {
prev2 = obj;
}
if (!%HaveSameMap(prev2, obj)) {
counter2++;
}
let result = 0;
for (let i = 0; i < N; i++) {
result += obj.a + obj.b
}
return result;
}
function test3(obj) {
if (!prev3) {
prev3 = obj;
}
if (!%HaveSameMap(prev3, obj)) {
counter3++;
}
let result = 0;
for (let i = 0; i < N; i++) {
result += obj.a + obj.b
}
return result;
}
function test4(obj) {
if (!prev4) {
prev4 = obj;
}
if (!%HaveSameMap(prev4, obj)) {
counter4++;
}
let result = 0;
for (let i = 0; i < N; i++) {
result += obj.a + obj.b
}
return result;
}
const objToCopy1 = { a: 1, b: 2 };
const objToCopy2 = { a: 1, b: 2 };
const objToCopy3 = { a: 1, b: 2 };
const objToCopy4 = { a: 1, b: 2 };
const objToCopy5 = { a: 1, b: 2 };
const startParse = Date.now();
for (let i = 0; i < N; ++i) {
test(JSON.parse(JSON.stringify(objToCopy1)));
test(JSON.parse(JSON.stringify(objToCopy2)));
test(JSON.parse(JSON.stringify(objToCopy3)));
test(JSON.parse(JSON.stringify(objToCopy4)));
test(JSON.parse(JSON.stringify(objToCopy5)));
}
console.log("test with PARSE:", Date.now() - startParse, "ms.");
console.log(`Optimization status + ${%GetOptimizationStatus(test)}`)
const startStructuredClone = Date.now();
for (let i = 0; i < N; ++i) {
test2(structuredClone(objToCopy1));
test2(structuredClone(objToCopy2));
test2(structuredClone(objToCopy3));
test2(structuredClone(objToCopy4));
test2(structuredClone(objToCopy5));
}
console.log("test with structuredClone:", Date.now() - startStructuredClone, "ms.");
console.log(`Optimization status ${%GetOptimizationStatus(test2)}`)
const startSpread = Date.now();
for (let i = 0; i < N; ++i) {
test3({...objToCopy1});
test3({...objToCopy2});
test3({...objToCopy3});
test3({...objToCopy4});
test3({...objToCopy5});
}
console.log("test with spread:", Date.now() - startSpread, "ms.");
console.log(`Optimization status ${%GetOptimizationStatus(test3)}`)
const startObjectAssign = Date.now();
for (let i = 0; i < N; ++i) {
test4(Object.assign({}, objToCopy1));
test4(Object.assign({}, objToCopy2));
test4(Object.assign({}, objToCopy3));
test4(Object.assign({}, objToCopy4));
test4(Object.assign({}, objToCopy5));
}
console.log("test with ObjectAssign:", Date.now() - startObjectAssign, "ms.");
console.log(`Optimization status ${%GetOptimizationStatus(test4)}`)
console.log(`Deopt counters`);
console.log({counter1, counter2, counter3, counter4})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment