map-has-string.js (3122B)
1 // Similar test as "cacheir/map-has-string.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 testConstant_same_map(n) { 20 var xs = ["a", "b"]; 21 var ys = ["c", "d"]; 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(testConstant_same_map); 35 36 function testComputed_same_map(n) { 37 var xs = ["a", "b"]; 38 var ys = ["c", "d"]; 39 var zs = [...xs, ...ys]; 40 var map = createMap(xs, n); 41 42 var N = 100; 43 var c = 0; 44 for (var i = 0; i < N; ++i) { 45 var z = zs[i & 3]; 46 z = String.fromCharCode(z.charCodeAt(0)); 47 if (map.has(z)) c++; 48 if (map.has(z)) c++; 49 } 50 assertEq(c, N); 51 } 52 runTest(testComputed_same_map); 53 54 function testRope_same_map(n) { 55 var xs = ["a", "b"]; 56 var ys = ["c", "d"]; 57 var zs = [...xs, ...ys]; 58 var map = createMap(xs.map(x => x.repeat(100)), n); 59 60 var N = 100; 61 var c = 0; 62 for (var i = 0; i < N; ++i) { 63 var z = zs[i & 3].repeat(100); 64 if (map.has(z)) c++; 65 if (map.has(z)) c++; 66 } 67 assertEq(c, N); 68 } 69 runTest(testRope_same_map); 70 71 // Duplicate the above tests, but this time use a different map. 72 73 function testConstant_different_map(n) { 74 var xs = ["a", "b"]; 75 var ys = ["c", "d"]; 76 var zs = [...xs, ...ys]; 77 var map1 = createMap(xs, n); 78 var map2 = createMap(xs, n); 79 80 var N = 100; 81 var c = 0; 82 for (var i = 0; i < N; ++i) { 83 var z = zs[i & 3]; 84 if (map1.has(z)) c++; 85 if (map2.has(z)) c++; 86 } 87 assertEq(c, N); 88 } 89 runTest(testConstant_different_map); 90 91 function testComputed_different_map(n) { 92 var xs = ["a", "b"]; 93 var ys = ["c", "d"]; 94 var zs = [...xs, ...ys]; 95 var map1 = createMap(xs, n); 96 var map2 = createMap(xs, n); 97 98 var N = 100; 99 var c = 0; 100 for (var i = 0; i < N; ++i) { 101 var z = zs[i & 3]; 102 z = String.fromCharCode(z.charCodeAt(0)); 103 if (map1.has(z)) c++; 104 if (map2.has(z)) c++; 105 } 106 assertEq(c, N); 107 } 108 runTest(testComputed_different_map); 109 110 function testRope_different_map(n) { 111 var xs = ["a", "b"]; 112 var ys = ["c", "d"]; 113 var zs = [...xs, ...ys]; 114 var map1 = createMap(xs.map(x => x.repeat(100)), n); 115 var map2 = createMap(xs.map(x => x.repeat(100)), n); 116 117 var N = 100; 118 var c = 0; 119 for (var i = 0; i < N; ++i) { 120 var z = zs[i & 3].repeat(100); 121 if (map1.has(z)) c++; 122 if (map2.has(z)) c++; 123 } 124 assertEq(c, N); 125 } 126 runTest(testRope_different_map); 127 128 // Test the alias information is correct. 129 130 function test_alias(n) { 131 var xs = ["a", "b"]; 132 var map = createMap([], n); 133 134 var N = 100; 135 var c = 0; 136 for (var i = 0; i < N; ++i) { 137 var x = xs[i & 1]; 138 139 map.set(x, x); 140 if (map.has(x)) c++; 141 142 map.delete(x); 143 if (map.has(x)) c++; 144 } 145 assertEq(c, N); 146 } 147 runTest(test_alias);