Skip to content

Commit 266cb15

Browse files
brandonocaseygkatsev
authored andcommitted
perf: Improve performance of toTitleCase, register with lower and TitleCase (#6148)
1 parent 8610f99 commit 266cb15

File tree

11 files changed

+51
-32
lines changed

11 files changed

+51
-32
lines changed

src/js/component.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as Dom from './utils/dom.js';
1010
import DomData from './utils/dom-data';
1111
import * as Fn from './utils/fn.js';
1212
import * as Guid from './utils/guid.js';
13-
import toTitleCase from './utils/to-title-case.js';
13+
import {toTitleCase, toLowerCase} from './utils/string-cases.js';
1414
import mergeOptions from './utils/merge-options.js';
1515
import computedStyle from './utils/computed-style';
1616

@@ -360,8 +360,6 @@ class Component {
360360
return;
361361
}
362362

363-
name = toTitleCase(name);
364-
365363
return this.childNameIndex_[name];
366364
}
367365

@@ -435,6 +433,7 @@ class Component {
435433

436434
if (componentName) {
437435
this.childNameIndex_[componentName] = component;
436+
this.childNameIndex_[toLowerCase(componentName)] = component;
438437
}
439438

440439
// Add the UI object's element to the container div (box)
@@ -483,7 +482,8 @@ class Component {
483482
component.parentComponent_ = null;
484483

485484
this.childIndex_[component.id()] = null;
486-
this.childNameIndex_[component.name()] = null;
485+
this.childNameIndex_[toTitleCase(component.name())] = null;
486+
this.childNameIndex_[toLowerCase(component.name())] = null;
487487

488488
const compEl = component.el();
489489

@@ -1545,6 +1545,7 @@ class Component {
15451545
}
15461546

15471547
Component.components_[name] = ComponentToRegister;
1548+
Component.components_[toLowerCase(name)] = ComponentToRegister;
15481549

15491550
return ComponentToRegister;
15501551
}
@@ -1564,15 +1565,11 @@ class Component {
15641565
* return that if it exists.
15651566
*/
15661567
static getComponent(name) {
1567-
if (!name) {
1568+
if (!name || !Component.components_) {
15681569
return;
15691570
}
15701571

1571-
name = toTitleCase(name);
1572-
1573-
if (Component.components_ && Component.components_[name]) {
1574-
return Component.components_[name];
1575-
}
1572+
return Component.components_[name];
15761573
}
15771574
}
15781575

src/js/control-bar/text-track-controls/chapters-button.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import TextTrackButton from './text-track-button.js';
55
import Component from '../../component.js';
66
import ChaptersTrackMenuItem from './chapters-track-menu-item.js';
7-
import toTitleCase from '../../utils/to-title-case.js';
7+
import {toTitleCase} from '../../utils/string-cases.js';
88

99
/**
1010
* The button component for toggling and selecting chapters

src/js/control-bar/text-track-controls/subs-caps-button.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import TextTrackButton from './text-track-button.js';
55
import Component from '../../component.js';
66
import CaptionSettingsMenuItem from './caption-settings-menu-item.js';
77
import SubsCapsMenuItem from './subs-caps-menu-item.js';
8-
import toTitleCase from '../../utils/to-title-case.js';
8+
import {toTitleCase} from '../../utils/string-cases.js';
99
/**
1010
* The button component for toggling and selecting captions and/or subtitles
1111
*

src/js/menu/menu-button.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Button from '../button.js';
55
import Component from '../component.js';
66
import Menu from './menu.js';
77
import * as Dom from '../utils/dom.js';
8-
import toTitleCase from '../utils/to-title-case.js';
8+
import {toTitleCase} from '../utils/string-cases.js';
99
import { IS_IOS } from '../utils/browser.js';
1010
import keycode from 'keycode';
1111

src/js/player.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import * as Guid from './utils/guid.js';
1717
import * as browser from './utils/browser.js';
1818
import {IE_VERSION, IS_CHROME, IS_WINDOWS} from './utils/browser.js';
1919
import log, { createLogger } from './utils/log.js';
20-
import toTitleCase, { titleCaseEquals } from './utils/to-title-case.js';
20+
import {toTitleCase, titleCaseEquals} from './utils/string-cases.js';
2121
import { createTimeRange } from './utils/time-ranges.js';
2222
import { bufferedPercent } from './utils/buffer.js';
2323
import * as stylesheet from './utils/stylesheet.js';

src/js/tech/html5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import document from 'global/document';
1111
import window from 'global/window';
1212
import {assign} from '../utils/obj';
1313
import mergeOptions from '../utils/merge-options.js';
14-
import toTitleCase from '../utils/to-title-case.js';
14+
import {toTitleCase} from '../utils/string-cases.js';
1515
import {NORMAL as TRACK_TYPES} from '../tracks/track-types';
1616
import setupSourceset from './setup-sourceset';
1717

src/js/tech/loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import Component from '../component.js';
55
import Tech from './tech.js';
6-
import toTitleCase from '../utils/to-title-case.js';
6+
import {toTitleCase} from '../utils/string-cases.js';
77
import mergeOptions from '../utils/merge-options.js';
88

99
/**

src/js/tech/middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @module middleware
44
*/
55
import { assign } from '../utils/obj.js';
6-
import toTitleCase from '../utils/to-title-case.js';
6+
import {toTitleCase} from '../utils/string-cases.js';
77

88
const middlewares = {};
99
const middlewareInstances = {};

src/js/tech/tech.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import window from 'global/window';
1313
import document from 'global/document';
1414
import {isPlain} from '../utils/obj';
1515
import * as TRACK_TYPES from '../tracks/track-types';
16-
import toTitleCase from '../utils/to-title-case';
16+
import {toTitleCase, toLowerCase} from '../utils/string-cases.js';
1717
import vtt from 'videojs-vtt.js';
1818

1919
/**
@@ -920,6 +920,7 @@ class Tech extends Component {
920920
name = toTitleCase(name);
921921

922922
Tech.techs_[name] = tech;
923+
Tech.techs_[toLowerCase(name)] = tech;
923924
if (name !== 'Tech') {
924925
// camel case the techName for use in techOrder
925926
Tech.defaultTechOrder_.push(name);
@@ -941,12 +942,12 @@ class Tech extends Component {
941942
return;
942943
}
943944

944-
name = toTitleCase(name);
945-
946945
if (Tech.techs_ && Tech.techs_[name]) {
947946
return Tech.techs_[name];
948947
}
949948

949+
name = toTitleCase(name);
950+
950951
if (window && window.videojs && window.videojs[name]) {
951952
log.warn(`The ${name} tech was added to the videojs object when it should be registered using videojs.registerTech(name, tech)`);
952953
return window.videojs[name];
Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
/**
2-
* @file to-title-case.js
3-
* @module to-title-case
2+
* @file string-cases.js
3+
* @module to-lower-case
44
*/
55

6+
/**
7+
* Lowercase the first letter of a string.
8+
*
9+
* @param {string} string
10+
* String to be lowercased
11+
*
12+
* @return {string}
13+
* The string with a lowercased first letter
14+
*/
15+
export const toLowerCase = function(string) {
16+
if (typeof string !== 'string') {
17+
return string;
18+
}
19+
20+
return string.replace(/./, (w) => w.toLowerCase());
21+
};
22+
623
/**
724
* Uppercase the first letter of a string.
825
*
@@ -12,15 +29,13 @@
1229
* @return {string}
1330
* The string with an uppercased first letter
1431
*/
15-
function toTitleCase(string) {
32+
export const toTitleCase = function(string) {
1633
if (typeof string !== 'string') {
1734
return string;
1835
}
1936

20-
return string.charAt(0).toUpperCase() + string.slice(1);
21-
}
22-
23-
export default toTitleCase;
37+
return string.replace(/./, (w) => w.toUpperCase());
38+
};
2439

2540
/**
2641
* Compares the TitleCase versions of the two strings for equality.
@@ -34,6 +49,6 @@ export default toTitleCase;
3449
* @return {boolean}
3550
* Whether the TitleCase versions of the strings are equal
3651
*/
37-
export function titleCaseEquals(str1, str2) {
52+
export const titleCaseEquals = function(str1, str2) {
3853
return toTitleCase(str1) === toTitleCase(str2);
39-
}
54+
};

0 commit comments

Comments
 (0)