v8/test/mjsunit/compiler/string-startswith.js
Ujjwal Sharma 094435b6b6 Reland "[turbofan] Add fast path for single-character String#startsWith()"
This is a reland of e7e512da66

Original change's description:
> [turbofan] Add fast path for single-character String#startsWith()
> 
> This CL adds a fast path to String#startsWith(s) if s is a
> single character string.
> 
> Bug: v8:8400
> Change-Id: Ibd6a9d1e46d98f41c198d2b579208e25003eedb0
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1525362
> Reviewed-by: Benedikt Meurer <bmeurer@chromium.org>
> Reviewed-by: Sigurd Schneider <sigurds@chromium.org>
> Commit-Queue: Sigurd Schneider <sigurds@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#61504}

Bug: v8:8400
Change-Id: Ic2d60ccb8fdeb51373fcd025a7e970fda0c14d79
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1618342
Reviewed-by: Sigurd Schneider <sigurds@chromium.org>
Commit-Queue: Sigurd Schneider <sigurds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61637}
2019-05-20 08:42:57 +00:00

82 lines
2.1 KiB
JavaScript

// Copyright 2019 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 --opt
(function() {
function foo(string) { return string.startsWith('a'); }
%PrepareFunctionForOptimization(foo);
assertEquals(false, foo(''));
assertEquals(true, foo('a'));
assertEquals(false, foo('ba'));
assertEquals(true, foo('abc'));
%OptimizeFunctionOnNextCall(foo);
assertEquals(false, foo(''));
assertEquals(true, foo('a'));
assertEquals(false, foo('ba'));
assertEquals(true, foo('abc'));
assertOptimized(foo);
})();
(function() {
function f() { return "abc".startsWith(); }
%PrepareFunctionForOptimization(f);
assertEquals(false, f());
assertEquals(false, f());
%OptimizeFunctionOnNextCall(f);
assertEquals(false, f());
assertOptimized(f);
})();
(function() {
function g(n) { return "abc".startsWith("a", n); }
%PrepareFunctionForOptimization(g);
assertEquals(true, g(-1));
assertEquals(true, g(0));
assertEquals(false, g(1));
assertEquals(false, g(2));
assertEquals(false, g(3));
assertEquals(false, g(4));
%OptimizeFunctionOnNextCall(g);
assertEquals(true, g(-1));
assertEquals(true, g(0));
assertEquals(false, g(1));
assertEquals(false, g(2));
assertEquals(false, g(3));
assertEquals(false, g(4));
assertOptimized(g);
})();
(function() {
function g(n) { return "cba".startsWith("a", n); }
%PrepareFunctionForOptimization(g);
assertEquals(false, g(-1));
assertEquals(false, g(0));
assertEquals(false, g(1));
assertEquals(true, g(2));
assertEquals(false, g(3));
assertEquals(false, g(4));
%OptimizeFunctionOnNextCall(g);
assertEquals(false, g(-1));
assertEquals(false, g(0));
assertEquals(false, g(1));
assertEquals(true, g(2));
assertEquals(false, g(3));
assertEquals(false, g(4));
assertOptimized(g);
})();
(function() {
function f(n) { return "cba".startsWith("a", n); }
%PrepareFunctionForOptimization(f);
f();
f();
%OptimizeFunctionOnNextCall(f);
assertEquals(false, f(1073741824));
})();