Nest: Using Iterare instead of Array chain methods

Created on 10 May 2017  路  4Comments  路  Source: nestjs/nest

I would like to recommend iterare for tranform a collection over using array chain methods.
Regards.

type

Most helpful comment

@alexjoverm fe:

from: instance-loader.ts

    private callModuleInitHook(module: Module) {
        const components = [...module.routes, ...module.components];
        components.map(([key, {instance}]) => instance)
                .filter((instance) => !isNil(instance))
                .filter(this.hasOnModuleInitHook)
                .forEach((instance) => (instance as OnModuleInit).onModuleInit());
    }

Depends on how many components are, this chain will be executed 4 times for N items.

So when we will use iterare:

    private callModuleInitHook(module: Module) {
        const components = [...module.routes, ...module.components];
        iterate(components).map(([key, {instance}]) => instance)
                .filter((instance) => !isNil(instance))
                .filter(this.hasOnModuleInitHook)
                .forEach((instance) => (instance as OnModuleInit).onModuleInit());
    }

Will only loop just once for N items. Slightly performance improvements. Bechmarks http://iterare.surge.sh/#performance shows the impact.

Regards.

All 4 comments

I don't understand this issue. Cannot one itself use iterate on its code? Is this issue meaning to use iterate in the core code of nest?

@alexjoverm fe:

from: instance-loader.ts

    private callModuleInitHook(module: Module) {
        const components = [...module.routes, ...module.components];
        components.map(([key, {instance}]) => instance)
                .filter((instance) => !isNil(instance))
                .filter(this.hasOnModuleInitHook)
                .forEach((instance) => (instance as OnModuleInit).onModuleInit());
    }

Depends on how many components are, this chain will be executed 4 times for N items.

So when we will use iterare:

    private callModuleInitHook(module: Module) {
        const components = [...module.routes, ...module.components];
        iterate(components).map(([key, {instance}]) => instance)
                .filter((instance) => !isNil(instance))
                .filter(this.hasOnModuleInitHook)
                .forEach((instance) => (instance as OnModuleInit).onModuleInit());
    }

Will only loop just once for N items. Slightly performance improvements. Bechmarks http://iterare.surge.sh/#performance shows the impact.

Regards.

Hi @cojack,
Since 2.0.0 iterate library is used. Thanks for your proposal :)

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings