set-has-string.js (1584B)
1 // Return a new set, possibly filling some dummy entries to enforce creating 2 // multiple hash buckets. 3 function createSet(values, n) { 4 var xs = [...values]; 5 for (var i = 0; i < n; ++i) { 6 xs.push({}); 7 } 8 return new Set(xs); 9 } 10 11 function runTest(fn) { 12 fn(0); 13 fn(100); 14 } 15 16 function testConstant(n) { 17 var xs = ["a", "b"]; 18 var ys = ["c", "d"]; 19 var zs = [...xs, ...ys]; 20 var set = createSet(xs, n); 21 22 var N = 100; 23 var c = 0; 24 for (var i = 0; i < N; ++i) { 25 var z = zs[i & 3]; 26 if (set.has(z)) c++; 27 } 28 assertEq(c, N / 2); 29 } 30 runTest(testConstant); 31 32 function testConstantFatInline(n) { 33 var xs = ["a", "b"].map(s => s.repeat(10)); 34 var ys = ["c", "d"].map(s => s.repeat(10)); 35 var zs = [...xs, ...ys]; 36 var set = createSet(xs, n); 37 38 var N = 100; 39 var c = 0; 40 for (var i = 0; i < N; ++i) { 41 var z = zs[i & 3]; 42 if (set.has(z)) c++; 43 } 44 assertEq(c, N / 2); 45 } 46 runTest(testConstantFatInline); 47 48 function testComputed(n) { 49 var xs = ["a", "b"]; 50 var ys = ["c", "d"]; 51 var zs = [...xs, ...ys]; 52 var set = createSet(xs, n); 53 54 var N = 100; 55 var c = 0; 56 for (var i = 0; i < N; ++i) { 57 var z = zs[i & 3]; 58 z = String.fromCharCode(z.charCodeAt(0)); 59 if (set.has(z)) c++; 60 } 61 assertEq(c, N / 2); 62 } 63 runTest(testComputed); 64 65 function testRope(n) { 66 var xs = ["a", "b"]; 67 var ys = ["c", "d"]; 68 var zs = [...xs, ...ys]; 69 var set = createSet(xs.map(x => x.repeat(100)), n); 70 71 var N = 100; 72 var c = 0; 73 for (var i = 0; i < N; ++i) { 74 var z = zs[i & 3].repeat(100); 75 if (set.has(z)) c++; 76 } 77 assertEq(c, N / 2); 78 } 79 runTest(testRope);