Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simpify getting object type name #7159

Merged
merged 2 commits into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 42 additions & 27 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have any perf benchmarks showing that this slice is not slowing down the whole caching effort?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slice(8, -1) works 4x faster than concatanation '[object ' + typeName + ']'.

Source of my test.

const map1 = new Map();
const map2 = new Map();
let start;

function isA(typeName, value) {
  return Object.prototype.toString.apply(value) === '[object ' + typeName + ']';
}

function getObjectType(value) {
  return Object.prototype.toString.apply(value).slice(8, -1);
}

console.log('isA');
console.log('=======================');

start = process.hrtime()[1];
const compare1 = isA('Map', map1);
console.log(process.hrtime()[1] - start, compare1);


console.log('');
console.log('getObjectType');
console.log('=======================');
start = process.hrtime()[1];
const compare2 = getObjectType(map1) === 'Map';
console.log(process.hrtime()[1] - start, compare2);

Result of my tests (nano seconds):

isA
=======================
208620 true

getObjectType
=======================
57268 true

Tested on Node v10.10.0, MacOs.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is single execution without a warmup, hard to tell if it's representative. Can your run it on jsperf? Something like here https://twitter.com/bmeurer/status/867277137564360704?s=21

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gain in jest's case is probably from doing the toString once instead of up to 12 times.

I doubt slice or not makes much of a difference. I don't think this is super hot code anyways - it's only used when generating mocks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. full test.
Test compares original getType(with many isA calls) and getType from this PR with chaching.

https://jsperf.com/object-prototype-tostring-caching

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's the result:

fwiw, performance aside, I think this code is easier to read and maintain so I'm +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';
Expand All @@ -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 {
Expand Down