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.Touch');
const utilsString = goog.require('Blockly.utils.string');
/* eslint-disable-next-line no-unused-vars */
const {Gesture} = goog.requireType('Blockly.Gesture');
const {globalThis} = goog.require('Blockly.utils.global');
@@ -246,7 +245,7 @@ exports.checkTouchIdentifier = checkTouchIdentifier;
* @alias Blockly.Touch.setClientFromTouch
*/
const setClientFromTouch = function(e) {
if (utilsString.startsWith(e.type, 'touch') && e.changedTouches) {
if (e.type.startsWith('touch') && e.changedTouches) {
// Map the touch event's properties to the event.
const touchPoint = e.changedTouches[0];
e.clientX = touchPoint.clientX;
@@ -263,9 +262,8 @@ exports.setClientFromTouch = setClientFromTouch;
* @alias Blockly.Touch.isMouseOrTouchEvent
*/
const isMouseOrTouchEvent = function(e) {
return utilsString.startsWith(e.type, 'touch') ||
utilsString.startsWith(e.type, 'mouse') ||
utilsString.startsWith(e.type, 'pointer');
return e.type.startsWith('touch') || e.type.startsWith('mouse') ||
e.type.startsWith('pointer');
};
exports.isMouseOrTouchEvent = isMouseOrTouchEvent;
@@ -276,8 +274,7 @@ exports.isMouseOrTouchEvent = isMouseOrTouchEvent;
* @alias Blockly.Touch.isTouchEvent
*/
const isTouchEvent = function(e) {
return utilsString.startsWith(e.type, 'touch') ||
utilsString.startsWith(e.type, 'pointer');
return e.type.startsWith('touch') || e.type.startsWith('pointer');
};
exports.isTouchEvent = isTouchEvent;

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;