chore: Make startsWith faster (#6118)

by using built-in function (polyfill will handle IE).
This commit is contained in:
Neil Fraser
2022-04-27 15:25:07 -07:00
committed by GitHub
parent b1bb5c53b8
commit 63ca83b545
7 changed files with 19 additions and 35 deletions

View File

@@ -15,7 +15,6 @@
goog.module('Blockly.utils.parsing');
const colourUtils = goog.require('Blockly.utils.colour');
const stringUtils = goog.require('Blockly.utils.string');
const {Msg} = goog.require('Blockly.Msg');
@@ -97,9 +96,8 @@ const tokenizeInterpolationInternal = function(
// BKY_ is the prefix used to namespace the strings used in Blockly
// core files and the predefined blocks in ../blocks/.
// These strings are defined in ../msgs/ files.
const bklyKey = stringUtils.startsWith(keyUpper, 'BKY_') ?
keyUpper.substring(4) :
null;
const bklyKey =
keyUpper.startsWith('BKY_') ? keyUpper.substring(4) : null;
if (bklyKey && bklyKey in Msg) {
const rawValue = Msg[bklyKey];
if (typeof rawValue === 'string') {

View File

@@ -19,17 +19,22 @@
*/
goog.module('Blockly.utils.string');
const deprecation = goog.require('Blockly.utils.deprecation');
/**
* Fast prefix-checker.
* Copied from Closure's goog.string.startsWith.
* Obsolete prefix-checker.
* @param {string} str The string to check.
* @param {string} prefix A string to look for at the start of `str`.
* @return {boolean} True if `str` begins with `prefix`.
* @alias Blockly.utils.string.startsWith
* @deprecated April 2022. Use built-in string.startsWith.
*/
const startsWith = function(str, prefix) {
return str.lastIndexOf(prefix, 0) === 0;
deprecation.warn(
'Blockly.utils.string.startsWith()', 'April 2022', 'April 2023',
'Use built-in string.startsWith');
return str.startsWith(prefix);
};
exports.startsWith = startsWith;