How can I perform operations in JavaScript just like we do pipeline of operations in Java streams?

前端 未结 7 1098
北海茫月
北海茫月 2020-12-18 20:07

In Java 8 using streams when I chain methods one after the another the execution of operations are performed in pipelined manner.

Example:

List

        
7条回答
  •  鱼传尺愫
    2020-12-18 20:30

    You kinda get the same output, as in the relative values of the sequence of map1, map2 and forEach values are the same.

    The difference in order that you're seeing shows the underlying difference between the machine models of the JVM and the JavaScript runtime engine.

    JVMs are threaded. JavaScript is not. That means, that your sequence steps in Java can run immediately after the critical number of map operations have occurred.

    In JavaScript, the next step is put at the bottom of the execute stack, and every operation at the top must first be executed before getting to the next items.

    As you can see, the methods are functionally equivalent, but have different mechanics.

提交回复
热议问题