From 2cca33ae3a46246c8c3c0ad8bf7111f5def3c405 Mon Sep 17 00:00:00 2001 From: mjgp2 Date: Tue, 7 Jun 2022 12:06:51 +0100 Subject: [PATCH] fix(core): Fix stripProtoKeys performance --- packages/common/pipes/validation.pipe.ts | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/common/pipes/validation.pipe.ts b/packages/common/pipes/validation.pipe.ts index 65d88ed4a57..8622fdcd287 100644 --- a/packages/common/pipes/validation.pipe.ts +++ b/packages/common/pipes/validation.pipe.ts @@ -1,4 +1,5 @@ import { iterate } from 'iterare'; +import { isTypedArray } from 'util/types'; import { Optional } from '../decorators'; import { Injectable } from '../decorators/core'; import { HttpStatus } from '../enums/http-status.enum'; @@ -190,12 +191,25 @@ export class ValidationPipe implements PipeTransform { return isNil(value) ? {} : value; } - protected stripProtoKeys(value: Record) { + protected stripProtoKeys(value: any) { + if ( isTypedArray(value) ) { + return; + } + if ( Array.isArray(value) ) { + for ( const v of value ) { + if ( typeof v === 'object' ) { + this.stripProtoKeys(v); + } + } + return; + } delete value.__proto__; - const keys = Object.keys(value); - iterate(keys) - .filter(key => isObject(value[key]) && value[key]) - .forEach(key => this.stripProtoKeys(value[key])); + for ( const key in value ) { + const v = value[key]; + if ( typeof v === 'object' ) { + this.stripProtoKeys(v); + } + } } protected isPrimitive(value: unknown): boolean {