angular/devtools/projects/ng-devtools-backend/src/lib/serialization-utils.spec.ts
AleksanderBodurri 32ed78bcbe fix(devtools): optimize object sanitization logic (#64234)
Previously this would take ~3500ms adev.

This updated logic avoids the constant JSON.stringify implementation and instead checks for serializable values directly.

After this change this code path for adev takes less than 20ms.

(Benchmarks taken on an M1 Macbook Pro)

PR Close #64234
2025-10-06 15:00:19 -04:00

75 lines
1.4 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {sanitizeObject} from './serialization-utils';
describe('sanitizeObject', () => {
it('should not change valid object', () => {
const foo = {
bar: 'bar',
baz: 42,
qux: true,
quux: null,
corge: undefined,
grault: [1, 2, 3],
garply: {a: 'a', b: 'b'},
};
expect(sanitizeObject(foo)).toEqual({
bar: 'bar',
baz: 42,
qux: true,
quux: null,
corge: '[Non-serializable data]',
grault: [1, 2, 3],
garply: {a: 'a', b: 'b'},
});
});
it('should remove function', () => {
const foo = {
bar: 'bar',
baz: () => 'baz',
};
expect(sanitizeObject(foo)).toEqual({
bar: 'bar',
baz: '[Non-serializable data]',
});
});
it('should remove nested function', () => {
const foo = {
bar: 'bar',
baz: {
qux: () => 'qux',
},
};
expect(sanitizeObject(foo)).toEqual({
bar: 'bar',
baz: {
qux: '[Non-serializable data]',
},
});
});
it('should strip cyclic references', () => {
const bar: any = {foo: null};
const foo = {
bar: bar,
};
bar.foo = foo;
expect(sanitizeObject(foo)).toEqual({
bar: {
foo: '[Circular]',
},
});
});
});