object-keys-02.js (1457B)
1 // This test case check some code frequently used in the wild, with some object 2 // (proxy) for which the optimization we have deployed do not work, as the 3 // assumption the Object.keys(...) can be elided without having noticeable 4 // side-effects. 5 6 // Similar functions are part of popular framework such as React and Angular. 7 function shallowEqual(o1, o2) { 8 var k1 = Object.keys(o1); 9 var k2 = Object.keys(o2); 10 if (k1.length != k2.length) { 11 return false; 12 } 13 for (var k = 0; k < k1.length; k++) { 14 if (!Object.hasOwnProperty.call(o2, k1[k]) || !Object.is(o1[k1[k]], o2[k1[k]])) { 15 return false; 16 } 17 } 18 return true; 19 } 20 21 let sideEffectCounter = 0; 22 const payload = {x: 5, a: 1, b: 2, c: 3, d: 4}; 23 const handler = { 24 ownKeys(target) { 25 // side-effect that should not be removed. 26 sideEffectCounter++; 27 // answer returned. 28 return Reflect.ownKeys(target); 29 }, 30 }; 31 const proxy = new Proxy(payload, handler); 32 33 let objs = [ 34 {x:0, a: 1, b: 2}, 35 {x:1, b: 1, c: 2}, 36 {x:2, c: 1, d: 2}, 37 {x:3, a: 1, b: 2, c: 3}, 38 {x:4, b: 1, c: 2, d: 3}, 39 proxy 40 ]; 41 42 with({}) {} 43 let iterMax = 1000; 44 for (let i = 0; i < iterMax; i++) { 45 for (let o1 of objs) { 46 for (let o2 of objs) { 47 assertEq(shallowEqual(o1, o2), Object.is(o1, o2)); 48 } 49 } 50 } 51 52 let expectedSideEffects = 2 * objs.length * iterMax; 53 assertEq(sideEffectCounter, expectedSideEffects);