Skip to content

Instantly share code, notes, and snippets.

@conartist6
Last active May 20, 2022
Embed
What would you like to do?
function sum(arr) {
let acc = 0;
for (let i = 0; i < arr.length; i++) {
acc += arr[i].val;
}
return acc;
}
const array1 = [];
const array2 = [];
const s = { val: 1 };
for (let i = 0; i < 100_000_000; i++) {
array1.push(s);
array2.push(s);
}
array1.push({ val: 0 }, { val: 1 }, { val: 2 }, { val: 3 });
array2.push({ val: 0, a: 0 }, { val: 1, b: 0 }, { val: 2, c: 0 }, { val: 3, d: 0 });
console.log('');
for (let i = 0; i < 5; i++) {
console.time('array1');
sum(array1)
console.timeEnd('array1');
}
console.log('');
for (let i = 0; i < 5; i++) {
console.time('array2');
sum(array2)
console.timeEnd('array2');
}
conartist6@mbp % node code.js
# Turbofan is running sum as bytecode
array1: 132.57ms
array1: 111.572ms
# Turbofan has optimized sum and so far the data is monomorphic
array1: 80.074ms
array1: 78.25ms
array1: 78.295ms
# All but the last 4 items in array2 are monomophic
array2: 78.558ms
# This run is deoptimized (to bytecode (why??))
array2: 111.462ms
# Turbofan has "optimized" sum again, but somehow failed horribly
array2: 343.306ms
array2: 343.86ms
array2: 342.094ms
conartist6@mbp % node --trace-deopt --trace-opt code.js
[marking 0x152e7a602a29 <JSFunction (sfi = 0x81037cf1b1)> for optimization to TURBOFAN, ConcurrencyMode::kConcurrent, reason: hot and stable]
[compiling method 0x152e7a602a29 <JSFunction (sfi = 0x81037cf1b1)> (target TURBOFAN) using TurboFan OSR]
[optimizing 0x152e7a602a29 <JSFunction (sfi = 0x81037cf1b1)> (target TURBOFAN) - took 0.000, 0.958, 0.000 ms]
[bailout (kind: deopt-soft, reason: Insufficient type feedback for call): begin. deoptimizing 0x3b21df5b9831 <JSFunction (sfi = 0x10c4d47127f9)>, opt id 0, bytecode offset 85, deopt exit 13, FP to SP delta 192, caller SP 0x00016ba1a9f0, pc 0x00010b18c438]
[marking 0x3b21df5b9ad9 <JSFunction sum (sfi = 0x10c4d4712831)> for optimization to TURBOFAN, ConcurrencyMode::kConcurrent, reason: small function]
[compiling method 0x3b21df5b9ad9 <JSFunction sum (sfi = 0x10c4d4712831)> (target TURBOFAN) using TurboFan OSR]
[optimizing 0x3b21df5b9ad9 <JSFunction sum (sfi = 0x10c4d4712831)> (target TURBOFAN) - took 0.000, 0.541, 0.000 ms]
array1: 115.701ms
[marking 0x3b21df5b9ad9 <JSFunction sum (sfi = 0x10c4d4712831)> for optimization to TURBOFAN, ConcurrencyMode::kConcurrent, reason: hot and stable]
[found optimized code for 0x3b21df5b9ad9 <JSFunction sum (sfi = 0x10c4d4712831)> (target TURBOFAN) at OSR bytecode offset 35]
array1: 113.721ms
[compiling method 0x3b21df5b9ad9 <JSFunction sum (sfi = 0x10c4d4712831)> (target TURBOFAN) using TurboFan]
[optimizing 0x3b21df5b9ad9 <JSFunction sum (sfi = 0x10c4d4712831)> (target TURBOFAN) - took 0.000, 0.500, 0.041 ms]
array1: 80.069ms
array1: 79.72ms
array1: 79.245ms
[bailout (kind: deopt-eager, reason: wrong map): begin. deoptimizing 0x3b21df5b9ad9 <JSFunction sum (sfi = 0x10c4d4712831)>, opt id 2, bytecode offset 4, deopt exit 5, FP to SP delta 64, caller SP 0x00016ba1a910, pc 0x00010b1673d0]
array2: 78.906ms
[marking 0x3b21df5b9ad9 <JSFunction sum (sfi = 0x10c4d4712831)> for optimization to TURBOFAN, ConcurrencyMode::kConcurrent, reason: hot and stable]
[found optimized code for 0x3b21df5b9ad9 <JSFunction sum (sfi = 0x10c4d4712831)> (target TURBOFAN) at OSR bytecode offset 35]
[bailout (kind: deopt-eager, reason: wrong map): begin. deoptimizing 0x3b21df5b9ad9 <JSFunction sum (sfi = 0x10c4d4712831)>, opt id 1, bytecode offset 4, deopt exit 11, FP to SP delta 128, caller SP 0x00016ba1a910, pc 0x00010b1670f8]
array2: 112.758ms
[compiling method 0x3b21df5b9ad9 <JSFunction sum (sfi = 0x10c4d4712831)> (target TURBOFAN) using TurboFan]
[optimizing 0x3b21df5b9ad9 <JSFunction sum (sfi = 0x10c4d4712831)> (target TURBOFAN) - took 0.000, 0.500, 0.042 ms]
array2: 350.273ms
array2: 351.822ms
array2: 357.311ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment