Node.js上的同步操作(大数据计算)

时间:2016-10-29 18:46:05

标签: javascript arrays node.js ecmascript-6 array-algorithms

修改

针对评论所做的更改

我有四个Arrays

commonOrdersList --length around 40k storeA --length close to 100k storeB --length around 110k storeC --length close to 100k

不同大小的对象看起来像

[
 {
  "orderId": "someUniqId",
  "orderTime": 1477764124822 //timestamp
 }
]

数组中没有特定的项目排序。

我需要做的是查找commonOrderlist中是否存在任何订单也存在于三个商店中的任何一个商店,即storeAstoreBstoreC并通过比较订单时间来过滤掉最近的订单。

这就像在比较三个不同大小的四个数组时获得联合。

我尝试对commonOrderList中的每个对象/订单进行迭代,然后将每个项目传递到其他商店,但这会导致内存堆栈大小超出范围错误

我做了类似

的事情
      function getRecentOrders () {
        commonOrderList.forEach(val => {
            storeA.forEach(item => {
              if (val.orderId === item.orderId && item.orderTime > val.orderTime) {
                    let orderDetails = {
                          "orderId": item.orderId,
                          "orderTime": item.orderTime
                    }
                    recentOrders.push(orderDetails);
                    uniqOrderIdSet.add(orderId);
            })
            storeB.forEach(item => {
              if (val.orderId === item.orderId && item.orderTime > val.orderTime) {
                   let orderDetails = {
                          "orderId": item.orderId,
                          "orderTime": item.orderTime
                    }
                    recentOrders.push(orderDetails);
                    uniqOrderIdSet.add(orderId);
            })
            storeC.forEach(item => {
              if (val.orderId === item.orderId && item.orderTime > val.orderTime) {
                    let orderDetails = {
                          "orderId": item.orderId,
                          "orderTime": item.orderTime
                    }
                    recentOrders.push(orderDetails);
                    uniqOrderIdSet.add(orderId);
            })
          })
      }

此超前内存调用堆栈大小

然后我尝试在Async setTimeout函数中放置上面的同步操作,以避免调用堆栈超出其限制,如:

      let recentOrders = [],
        uniqOrderIdSet = new Set();

      commonOrderList.forEach(val => {

        getOrdersFromStoreA(val);
        getOrdersFromStoreB(val);
        getOrdersFromStoreC(val);

      })

      function getOrdersFromStoreA(val) {
        setTimeout(() => {
          storeA.forEach(item => {
            if (val.orderId === item.orderId && item.orderTime > val.orderTime) {
              let orderDetails = {
                "orderId": item.orderId,
                "orderTime": item.orderTime
              }
              recentOrders.push(orderDetails);
              uniqOrderIdSet.add(orderId);
          })

        }, 6000);
      }

      function getOrdersFromStoreB(val) {
        setTimeout(() => {
          storeB.forEach(item => {
            if (val.orderId === item.orderId && item.orderTime > val.orderTime) {
               let orderDetails = {
                "orderId": item.orderId,
                "orderTime": item.orderTime
              }
              recentOrders.push(orderDetails);
              uniqOrderIdSet.add(orderId);
          })

        }, 3000);
      }

      function getOrdersFromStoreC(val) {
        setTimeout(() => {
          storeC.forEach(item => {
            if (val.orderId === item.orderId && item.orderTime > val.orderTime) {
               let orderDetails = {
                "orderId": item.orderId,
                "orderTime": item.orderTime
              }
              recentOrders.push(orderDetails);
              uniqOrderIdSet.add(orderId);
          })

        }, 8000);
      }

但是调用Stack再次超出。这也是耗时的过程

我还尝试创建 orderId MAP orderTime 但是可能有相同orderId和不同orderTime的项目,所以我也需要考虑这种情况。

那么有什么方法可以在更少的时间中执行此计算并且不会超出内存堆栈大小

任何形式的帮助都将受到赞赏。

这是我得到的错误堆栈:

{"name":"retail","hostname":"user-lenovo- 80","pid":26525,"level":20,"msg":"Error occurred in getRecentOrders() ===>     [
RangeError: Maximum call stack size exceeded]","time":"2016-10-28T10:48:59.668Z","v":0}
      RangeError: Maximum call stack size exceeded
          at process.nextTick (node.js:478:22)
          at onwrite (_stream_writable.js:343:15)
          at WritableState.onwrite (_stream_writable.js:89:5)
          at WriteStream.Socket._writeGeneric (net.js:690:5)
          at WriteStream.Socket._write (net.js:700:8)
          at doWrite (_stream_writable.js:300:12)
          at writeOrBuffer (_stream_writable.js:286:5)
          at WriteStream.Writable.write (_stream_writable.js:214:11)
          at WriteStream.Socket.write (net.js:626:40)
          at Console.log (console.js:39:16)

0 个答案:

没有答案