Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanmathis committed Dec 1, 2023
1 parent 3ebd400 commit 0723619
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
6 changes: 2 additions & 4 deletions packages/js-toolkit/utils/string/endsWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
* @see https://jsbench.me/1hlkqqd0ff/2
*/
export function endsWith(str: string, search: string): boolean {
const { length } = search;

if (length === 0) {
if (search.length === 0) {
return true;
}

if (length === 1) {
if (search.length === 1) {
// eslint-disable-next-line unicorn/prefer-at
return str[str.length - 1] === search;
}
Expand Down
8 changes: 3 additions & 5 deletions packages/js-toolkit/utils/string/startsWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
* @see https://jsbench.me/1hlkqqd0ff/1
*/
export function startsWith(str: string, search: string): boolean {
const { length } = search;

if (length === 0) {
if (search.length === 0) {
return true;
}

if (length === 1) {
if (search.length === 1) {
return str[0] === search;
}

// eslint-disable-next-line unicorn/prefer-string-slice
return str.substring(0, length) === search;
return str.substring(0, search.length) === search;
}

0 comments on commit 0723619

Please sign in to comment.