Skip to content

Commit

Permalink
refactor: enhance require profermance (#16)
Browse files Browse the repository at this point in the history
```js
console.time('sdk-base');
require('sdk-base');
console.timeEnd('sdk-base');
```

优化前

```bash
sdk-base: 14.602ms
```

优化后

```bash
sdk-base: 4.635ms
```

Co-authored-by: fengmk2 <[email protected]>
  • Loading branch information
gxcsoccer and fengmk2 authored Dec 3, 2022
1 parent 07d55e8 commit e9bf6e9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
24 changes: 17 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,37 @@

const co = require('co');
const util = require('util');
const is = require('is-type-of');
const assert = require('assert');
const awaitEvent = require('await-event');
const awaitFirst = require('await-first');
const EventEmitter = require('events').EventEmitter;
const CLOSE_PROMISE = Symbol('base#closePromise');

function isGeneratorFunction(obj) {
return obj &&
obj.constructor &&
obj.constructor.name === 'GeneratorFunction';
}

function isPromise(obj) {
return obj &&
typeof obj.then === 'function';
}

class Base extends EventEmitter {
constructor(options) {
super();

if (options && options.initMethod) {
assert(is.function(this[options.initMethod]),
assert(typeof this[options.initMethod] === 'function',
`[sdk-base] this.${options.initMethod} should be a function.`);

process.nextTick(() => {
if (is.generatorFunction(this[options.initMethod])) {
if (isGeneratorFunction(this[options.initMethod])) {
this[options.initMethod] = co.wrap(this[options.initMethod]);
}
const ret = this[options.initMethod]();
assert(is.promise(ret), `[sdk-base] this.${options.initMethod} should return either a promise or a generator`);
assert(isPromise(ret), `[sdk-base] this.${options.initMethod} should return either a promise or a generator`);
ret.then(() => this.ready(true))
.catch(err => this.ready(err));
});
Expand All @@ -41,7 +51,7 @@ class Base extends EventEmitter {
}

_wrapListener(eventName, listener) {
if (is.generatorFunction(listener)) {
if (isGeneratorFunction(listener)) {
assert(eventName !== 'error', '[sdk-base] `error` event should not have a generator listener.');

const newListener = (...args) => {
Expand Down Expand Up @@ -80,7 +90,7 @@ class Base extends EventEmitter {

removeListener(eventName, listener) {
let target = listener;
if (is.generatorFunction(listener)) {
if (isGeneratorFunction(listener)) {
const listeners = this.listeners(eventName);
for (const fn of listeners) {
if (fn.original === listener) {
Expand Down Expand Up @@ -124,7 +134,7 @@ class Base extends EventEmitter {
}
});
});
} else if (is.function(flagOrFunction)) {
} else if (typeof flagOrFunction === 'function') {
this._readyCallbacks.push(flagOrFunction);
} else if (flagOrFunction instanceof Error) {
this._ready = false;
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
"dependencies": {
"await-event": "^2.1.0",
"await-first": "^1.0.0",
"co": "^4.6.0",
"is-type-of": "^1.2.1"
"co": "^4.6.0"
},
"devDependencies": {
"@types/node": "^10.5.3",
Expand Down
11 changes: 8 additions & 3 deletions test/fixtures/ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
"target": "es6",
"baseUrl": ".",
"module": "commonjs",
"lib": [ "es7" ],
"lib": ["es7"],
"esModuleInterop": false,
"strict": true
}
"strict": true,
"noImplicitAny": false,
"skipLibCheck": true
},
"exclude": [
"node_modules"
]
}
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ describe('sdk-base', () => {
client.on('error', function* (err) {
console.error(err);
});
}, /\[sdk-base] `error` event should not have a generator listener./);
}, null, /\[sdk-base\] `error` event should not have a generator listener\./);
});
});

Expand Down

0 comments on commit e9bf6e9

Please sign in to comment.