v8/test/mjsunit/compiler/collection-has.js
snek 4c81827c8d optimize Set#has
Code for map methods was added a really long time ago but no one ever
brought that to set. Adds new common lowering for both collections and
updates the SetPrototypeHas builtin. My initial testing shows this to
be as much as 50x faster in some cases.

Change-Id: Ifea5be01c9e51013d57ac00bd817759ceace6669
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3709246
Reviewed-by: Toon Verwaest <verwaest@chromium.org>
Reviewed-by: Maya Lekova <mslekova@chromium.org>
Commit-Queue: snek <snek@chromium.org>
Cr-Commit-Position: refs/heads/main@{#81330}
2022-06-23 13:42:17 +00:00

78 lines
1.4 KiB
JavaScript

// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --allow-natives-syntax
(function() {
const map = new Map();
map.set(true, true);
for (let i = 0; i < 10000; i += 1) {
map.set(i, i);
map.set(`${i} number`, i);
}
function foo(x) {
return map.has(x);
}
%PrepareFunctionForOptimization(foo);
assertFalse(foo(1.5));
assertTrue(foo(1));
assertFalse(foo('1.5 number'));
assertTrue(foo('1 number'));
assertFalse(foo(false));
assertTrue(foo(true));
%OptimizeFunctionOnNextCall(foo);
assertFalse(foo(1.5));
assertTrue(foo(1));
assertFalse(foo('1.5 number'));
assertTrue(foo('1 number'));
assertFalse(foo(false));
assertTrue(foo(true));
})();
(function() {
const set = new Set();
set.add(true);
for (let i = 0; i < 10000; i += 1) {
set.add(i);
set.add(`${i} number`);
}
function foo(x) {
return set.has(x);
}
%PrepareFunctionForOptimization(foo);
assertFalse(foo(1.5));
assertTrue(foo(1));
assertFalse(foo('1.5 number'));
assertTrue(foo('1 number'));
assertFalse(foo(false));
assertTrue(foo(true));
%OptimizeFunctionOnNextCall(foo);
assertFalse(foo(1.5));
assertTrue(foo(1));
assertFalse(foo('1.5 number'));
assertTrue(foo('1 number'));
assertFalse(foo(false));
assertTrue(foo(true));
})();