Skip to content

Commit 15ab761

Browse files
authored
feat(utils): Try to optimize bundle size around type checks (#4300)
1 parent 1397a0a commit 15ab761

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

packages/utils/src/is.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
33

44
import { Primitive } from '@sentry/types';
5+
6+
// eslint-disable-next-line @typescript-eslint/unbound-method
7+
const objectToString = Object.prototype.toString;
8+
59
/**
610
* Checks whether given value's type is one of a few Error or Error-like
711
* {@link isError}.
@@ -10,18 +14,20 @@ import { Primitive } from '@sentry/types';
1014
* @returns A boolean representing the result.
1115
*/
1216
export function isError(wat: unknown): boolean {
13-
switch (Object.prototype.toString.call(wat)) {
17+
switch (objectToString.call(wat)) {
1418
case '[object Error]':
15-
return true;
1619
case '[object Exception]':
17-
return true;
1820
case '[object DOMException]':
1921
return true;
2022
default:
2123
return isInstanceOf(wat, Error);
2224
}
2325
}
2426

27+
function isBuiltin(wat: unknown, ty: string): boolean {
28+
return objectToString.call(wat) === `[object ${ty}]`;
29+
}
30+
2531
/**
2632
* Checks whether given value's type is ErrorEvent
2733
* {@link isErrorEvent}.
@@ -30,7 +36,7 @@ export function isError(wat: unknown): boolean {
3036
* @returns A boolean representing the result.
3137
*/
3238
export function isErrorEvent(wat: unknown): boolean {
33-
return Object.prototype.toString.call(wat) === '[object ErrorEvent]';
39+
return isBuiltin(wat, 'ErrorEvent');
3440
}
3541

3642
/**
@@ -41,7 +47,7 @@ export function isErrorEvent(wat: unknown): boolean {
4147
* @returns A boolean representing the result.
4248
*/
4349
export function isDOMError(wat: unknown): boolean {
44-
return Object.prototype.toString.call(wat) === '[object DOMError]';
50+
return isBuiltin(wat, 'DOMError');
4551
}
4652

4753
/**
@@ -52,7 +58,7 @@ export function isDOMError(wat: unknown): boolean {
5258
* @returns A boolean representing the result.
5359
*/
5460
export function isDOMException(wat: unknown): boolean {
55-
return Object.prototype.toString.call(wat) === '[object DOMException]';
61+
return isBuiltin(wat, 'DOMException');
5662
}
5763

5864
/**
@@ -63,7 +69,7 @@ export function isDOMException(wat: unknown): boolean {
6369
* @returns A boolean representing the result.
6470
*/
6571
export function isString(wat: unknown): boolean {
66-
return Object.prototype.toString.call(wat) === '[object String]';
72+
return isBuiltin(wat, 'String');
6773
}
6874

6975
/**
@@ -85,7 +91,7 @@ export function isPrimitive(wat: unknown): wat is Primitive {
8591
* @returns A boolean representing the result.
8692
*/
8793
export function isPlainObject(wat: unknown): wat is Record<string, unknown> {
88-
return Object.prototype.toString.call(wat) === '[object Object]';
94+
return isBuiltin(wat, 'Object');
8995
}
9096

9197
/**
@@ -118,7 +124,7 @@ export function isElement(wat: unknown): wat is Element {
118124
* @returns A boolean representing the result.
119125
*/
120126
export function isRegExp(wat: unknown): wat is RegExp {
121-
return Object.prototype.toString.call(wat) === '[object RegExp]';
127+
return isBuiltin(wat, 'RegExp');
122128
}
123129

124130
/**

packages/utils/src/object.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ export function normalizeToSize<T>(
186186
* unchanged.
187187
*/
188188
function serializeValue(value: any): any {
189-
const type = Object.prototype.toString.call(value);
190-
191189
// Node.js REPL notation
192190
if (typeof value === 'string') {
193191
return value;
194192
}
193+
194+
const type = Object.prototype.toString.call(value);
195195
if (type === '[object Object]') {
196196
return '[Object]';
197197
}

0 commit comments

Comments
 (0)