Skip to content

Commit

Permalink
chore: Make startsWith faster (#6118)
Browse files Browse the repository at this point in the history
by using built-in function (polyfill will handle IE).
  • Loading branch information
NeilFraser authored Apr 27, 2022
1 parent b1bb5c5 commit 63ca83b
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 35 deletions.
11 changes: 4 additions & 7 deletions core/touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;

Expand Down
6 changes: 2 additions & 4 deletions core/utils/parsing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');


Expand Down Expand Up @@ -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') {
Expand Down
11 changes: 8 additions & 3 deletions core/utils/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions scripts/gulpfiles/chunks.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@
"./core/renderers/zelos/measurables/row_elements.js",
"./core/renderers/zelos/marker_svg.js",
"./core/renderers/zelos/measurables/inputs.js",
"./core/renderers/zelos/path_object.js",
"./core/renderers/zelos/drawer.js",
"./core/renderers/zelos/renderer.js",
"./core/renderers/zelos/path_object.js",
"./core/field_textinput.js",
"./core/field_image.js",
"./core/renderers/zelos/constants.js",
Expand Down Expand Up @@ -229,7 +229,6 @@
"./core/events/events_bubble_open.js",
"./core/procedures.js",
"./core/workspace_svg.js",
"./core/utils/rect.js",
"./core/utils/deprecation.js",
"./core/utils/svg_math.js",
"./core/bubble_dragger.js",
Expand All @@ -242,6 +241,7 @@
"./core/tooltip.js",
"./core/block_svg.js",
"./core/utils/size.js",
"./core/utils/rect.js",
"./core/utils/coordinate.js",
"./core/utils/style.js",
"./core/dropdowndiv.js",
Expand Down
4 changes: 2 additions & 2 deletions tests/deps.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions tests/mocha/utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,6 @@ suite('Utils', function() {
});

suite('String', function() {
test('starts with', function() {
chai.assert.isFalse(Blockly.utils.string.startsWith('123', '2'), 'Does not start with');
chai.assert.isTrue(Blockly.utils.string.startsWith('123', '12'), 'Start with');
chai.assert.isTrue(Blockly.utils.string.startsWith('123', ''), 'Start with empty string 1');
chai.assert.isTrue(Blockly.utils.string.startsWith('', ''), 'Start with empty string 12');
});

test('shortest string length', function() {
let len = Blockly.utils.string.shortestStringLength('one,two,three,four,five'.split(','));
chai.assert.equal(len, 3, 'Length of "one"');
Expand Down
11 changes: 1 addition & 10 deletions typings/blockly.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,6 @@ declare module "core/utils/colour" {
export function hueToHex(hue: number): string;
}
declare module "core/utils/string" {
/**
* Fast prefix-checker.
* Copied from Closure's goog.string.startsWith.
* @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
*/
export function startsWith(str: string, prefix: string): boolean;
/**
* Given an array of strings, return the length of the shortest one.
* @param {!Array<string>} array Array of strings.
Expand Down Expand Up @@ -8041,7 +8032,7 @@ declare module "core/renderers/common/constants" {
* colourTertiary:(string|undefined),
* hat:(string|undefined)
* }} blockStyle A full or partial block style object.

* @return {!Theme.BlockStyle} A full block style object, with all
* required properties populated.
* @protected
Expand Down

0 comments on commit 63ca83b

Please sign in to comment.