moleculer icon indicating copy to clipboard operation
moleculer copied to clipboard

[lodash] Remove usage of _.forIn

Open abdavid opened this issue 5 years ago • 5 comments

See #433 for context.

Describe the solution you'd like Remove usage of _.forIn throughout Moleculer.

Describe alternatives you've considered Alternatives:

// 1
for (variable of iterable) {
  statement
}

// 2
for (variable in object)
  statement

// 3
for await (variable of asyncIterable) {
  statement
}

... and a myriad of other natively supported patterns which could be used depending on the context.

Additional context https://github.com/moleculerjs/moleculer/search?q=.forIn&unscoped_q=.forIn

abdavid avatar May 22 '20 19:05 abdavid

What is the difference between the first two alternatives?

By the way, we can't use the third because we don't use async/await in the core modules because await is slower then Promise, and performance is important in Moleculer :)

icebob avatar May 22 '20 19:05 icebob

What is the difference between the first two alternatives?

By the way, we can't use the third because we don't use async/await in the core modules because await is slower then Promise, and performance is important in Moleculer :)

Alternative 1 is for objects that implement the iterable interface. Arrays, strings etc

> for(const a of "abcdefg")
... console.log(a)
a
b
c
d
e
f
g

> for(const a of [1,2,3,4,5])
... console.log(a)
1
2
3
4
5

> for(const a of {a:1,b:2,c:3})
... console.log(a)
Thrown:
TypeError: {(intermediate value)(intermediate value)(intermediate value)} is not iterable

Alternative two is for "POJOs"

> for(const a in {a:1,b:2,c:3})
... console.log(a)
a
b
c
// for in with Iterables "a" is index.
> for(const a in [1,2,3,4,5])
... console.log(a)
0
1
2
3
4

abdavid avatar May 22 '20 19:05 abdavid

Some considerations aswell:

https://jsperf.com/for-of-vs-for-loop https://jsperf.com/for-vs-for-in

abdavid avatar May 23 '20 06:05 abdavid

Yeah, the native for(;;) is the quickest, but we need to use better performance solution in the critical parts (action calling, event sending, transfer packets....etc).

So if there is a loop which processes something from the broker options only once at the startup, we can use any solution because it is not cut down the runtime performance.

icebob avatar May 23 '20 06:05 icebob

BTW: https://v8.dev/blog/fast-async

Wallacy avatar May 23 '20 20:05 Wallacy