object-keys-03.js (1568B)
1 // This test case checks that some code can be optimized for non-proxy object, 2 // and than we can correctly fallback if a proxy object ever flow into this 3 // code. 4 5 // Similar functions are part of popular framework such as React and Angular. 6 function shallowEqual(o1, o2) { 7 var k1 = Object.keys(o1); 8 var k2 = Object.keys(o2); 9 if (k1.length != k2.length) { 10 return false; 11 } 12 for (var k = 0; k < k1.length; k++) { 13 if (!Object.hasOwnProperty.call(o2, k1[k]) || !Object.is(o1[k1[k]], o2[k1[k]])) { 14 return false; 15 } 16 } 17 return true; 18 } 19 20 let sideEffectCounter = 0; 21 const payload = {x: 5, a: 1, b: 2, c: 3, d: 4}; 22 const handler = { 23 ownKeys(target) { 24 // side-effect that should not be removed. 25 sideEffectCounter++; 26 // answer returned. 27 return Reflect.ownKeys(target); 28 }, 29 }; 30 const proxy = new Proxy(payload, handler); 31 32 let objs = [ 33 {x:0, a: 1, b: 2}, 34 {x:1, b: 1, c: 2}, 35 {x:2, c: 1, d: 2}, 36 {x:3, a: 1, b: 2, c: 3}, 37 {x:4, b: 1, c: 2, d: 3}, 38 {x:5, a: 1, b: 2, c: 3, d: 4} 39 ]; 40 41 // Ion compile shallowEqual ... 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 assertEq(sideEffectCounter, 0); 53 54 // ... before calling it with a proxy. 55 // This should bailout with a guard failure. 56 shallowEqual(objs[0], proxy); 57 58 // Assert that the proxy's ownKeys function has been called. 59 assertEq(sideEffectCounter, 1);