map-has-object.js (1872B)
1 // Similar test as "cacheir/map-has-object.js", except that we now perform 2 // duplicate lookups to ensure GVN works properly. 3 4 // Return a new map, possibly filling some dummy entries to enforce creating 5 // multiple hash buckets. 6 function createMap(values, n) { 7 var xs = [...values]; 8 for (var i = 0; i < n; ++i) { 9 xs.push({}); 10 } 11 return new Map(xs.map((x, i) => [x, i])); 12 } 13 14 function runTest(fn) { 15 fn(0); 16 fn(100); 17 } 18 19 function test_same_map(n) { 20 var xs = [{}, {}]; 21 var ys = [{}, {}]; 22 var zs = [...xs, ...ys]; 23 var map = createMap(xs, n); 24 25 var N = 100; 26 var c = 0; 27 for (var i = 0; i < N; ++i) { 28 var z = zs[i & 3]; 29 if (map.has(z)) c++; 30 if (map.has(z)) c++; 31 } 32 assertEq(c, N); 33 } 34 runTest(test_same_map); 35 36 // Duplicate the above tests, but this time use a different map. 37 38 function test_different_map(n) { 39 var xs = [{}, {}]; 40 var ys = [{}, {}]; 41 var zs = [...xs, ...ys]; 42 var map1 = createMap(xs, n); 43 var map2 = createMap(xs, n); 44 45 var N = 100; 46 var c = 0; 47 for (var i = 0; i < N; ++i) { 48 var z = zs[i & 3]; 49 if (map1.has(z)) c++; 50 if (map2.has(z)) c++; 51 } 52 assertEq(c, N); 53 } 54 runTest(test_different_map); 55 56 // Test the alias information is correct. 57 58 function test_alias(n) { 59 var xs = [{}, {}]; 60 var map = createMap([], n); 61 62 var N = 100; 63 var c = 0; 64 for (var i = 0; i < N; ++i) { 65 var x = xs[i & 1]; 66 67 map.set(x, x); 68 if (map.has(x)) c++; 69 70 map.delete(x); 71 if (map.has(x)) c++; 72 } 73 assertEq(c, N); 74 } 75 runTest(test_alias); 76 77 // And finally test that we don't actually support GVN for objects, because the 78 // hash changes when moving an object. 79 80 function testRekey() { 81 var map = new Map(); 82 var c = 0; 83 var N = 100; 84 for (var i = 0; i < N; ++i) { 85 var k = {}; 86 map.set(k, i); 87 88 if (map.has(k)) c++; 89 90 minorgc(); 91 92 if (map.has(k)) c++; 93 } 94 95 assertEq(c, N * 2); 96 } 97 testRekey();