From 3edd13eafe9b48004b30f7eabf1e287197422499 Mon Sep 17 00:00:00 2001 From: Mihail Bodrov Date: Sun, 14 Oct 2018 02:07:21 +0300 Subject: [PATCH 1/2] Simpify getting object type name --- packages/jest-mock/src/index.js | 69 ++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/packages/jest-mock/src/index.js b/packages/jest-mock/src/index.js index e97ccbcfb3c9..4bde745a3c25 100644 --- a/packages/jest-mock/src/index.js +++ b/packages/jest-mock/src/index.js @@ -173,31 +173,36 @@ function matchArity(fn: any, length: number): any { return mockConstructor; } -function isA(typeName: string, value: any): boolean { - return Object.prototype.toString.apply(value) === '[object ' + typeName + ']'; +function getObjectType(value: any): string { + return Object.prototype.toString.apply(value).slice(8, -1); } function getType(ref?: any): string | null { + const typeName = getObjectType(ref); if ( - isA('Function', ref) || - isA('AsyncFunction', ref) || - isA('GeneratorFunction', ref) + typeName === 'Function' || + typeName === 'AsyncFunction' || + typeName === 'GeneratorFunction' ) { return 'function'; } else if (Array.isArray(ref)) { return 'array'; - } else if (isA('Object', ref)) { + } else if (typeName === 'Object') { return 'object'; } else if ( - isA('Number', ref) || - isA('String', ref) || - isA('Boolean', ref) || - isA('Symbol', ref) + typeName === 'Number' || + typeName === 'String' || + typeName === 'Boolean' || + typeName === 'Symbol' ) { return 'constant'; - } else if (isA('Map', ref) || isA('WeakMap', ref) || isA('Set', ref)) { + } else if ( + typeName === 'Map' || + typeName === 'WeakMap' || + typeName === 'Set' + ) { return 'collection'; - } else if (isA('RegExp', ref)) { + } else if (typeName === 'RegExp') { return 'regexp'; } else if (ref === undefined) { return 'undefined'; @@ -209,21 +214,31 @@ function getType(ref?: any): string | null { } function isReadonlyProp(object: any, prop: string): boolean { - return ( - ((prop === 'arguments' || - prop === 'caller' || - prop === 'callee' || - prop === 'name' || - prop === 'length') && - (isA('Function', object) || - isA('AsyncFunction', object) || - isA('GeneratorFunction', object))) || - ((prop === 'source' || - prop === 'global' || - prop === 'ignoreCase' || - prop === 'multiline') && - isA('RegExp', object)) - ); + if ( + prop === 'arguments' || + prop === 'caller' || + prop === 'callee' || + prop === 'name' || + prop === 'length' + ) { + const typeName = getObjectType(object); + return ( + typeName === 'Function' || + typeName === 'AsyncFunction' || + typeName === 'GeneratorFunction' + ); + } + + if ( + prop === 'source' || + prop === 'global' || + prop === 'ignoreCase' || + prop === 'multiline' + ) { + return getObjectType(object) === 'RegExp'; + } + + return false; } class ModuleMockerClass { From e3f4643baaa09ee7a58d308a0dee3469db50c71e Mon Sep 17 00:00:00 2001 From: Mihail Bodrov Date: Mon, 15 Oct 2018 13:39:31 +0300 Subject: [PATCH 2/2] Add info to changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60d469eae6f9..c8354209adfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,10 @@ - `[docs]` Removed useless expect.assertions in `TestingAsyncCode.md` ([#7131](https://github.com/facebook/jest/pull/7131)) - `[docs]` Remove references to `@providesModule` which isn't supported anymore ([#7147](https://github.com/facebook/jest/pull/7147)) +### Performance + +- `[jest-mock]` Improve `getType` function performance. ([#7159](https://github.com/facebook/jest/pull/7159)) + ## 23.6.0 ### Features