当前位置: 首页 > news >正文

lodash中foreach踩坑

什么是lodash

Lodash 是一个 JavaScript 实用工具库,提供了很多用于处理数据、简化开发等方面的功能。它提供了一组常用的工具函数,用于处理数组、对象、字符串等常见数据结构,同时也包含了一些函数式编程的工具。对于前端开发来说,是个很好用的工具,甚至看过​有人说面试不会lodash被嘲讽了。

我所在的公司项目中也用了这个库,今天就听同事吐槽,他循环两万条数据,进入卡住了溢出了,找了半天问题,最后没想到是foreach的过,换成for循环就没问题了​。

那么为什么呢,lodash是怎么实现foreach的呢​?源码如下:

import arrayEach from './.internal/arrayEach.js';
import baseEach from './.internal/baseEach.js';/*** Iterates over elements of `collection` and invokes `iteratee` for each element.* The iteratee is invoked with three arguments: (value, index|key, collection).* Iteratee functions may exit iteration early by explicitly returning `false`.** **Note:** As with other "Collections" methods, objects with a "length"* property are iterated like arrays. To avoid this behavior use `forIn`* or `forOwn` for object iteration.** @since 0.1.0* @alias each* @category Collection* @param {Array|Object} collection The collection to iterate over.* @param {Function} iteratee The function invoked per iteration.* @returns {Array|Object} Returns `collection`.* @see forEachRight, forIn, forInRight, forOwn, forOwnRight* @example** forEach([1, 2], value => console.log(value))* // => Logs `1` then `2`.** forEach({ 'a': 1, 'b': 2 }, (value, key) => console.log(key))* // => Logs 'a' then 'b' (iteration order is not guaranteed).*/
function forEach(collection, iteratee) {const func = Array.isArray(collection) ? arrayEach : baseEach;return func(collection, iteratee);
}export default forEach;

源码感兴趣可以去github上看

以下是一个对 Lodash forEach 源码的简要分析:

function forEach(collection, iteratee) {const func = Array.isArray(collection) ? arrayEach : baseEach;return func(collection, iteratee);
}function arrayEach(array, iteratee) {let index = -1;const length = array == null ? 0 : array.length;while (++index < length) {if (iteratee(array[index], index, array) === false) {break;}}return array;
}function baseEach(collection, iteratee) {let index = -1;const iterable = Object(collection);const length = iterable.length;while (++index < length) {if (iteratee(iterable[index], index, iterable) === false) {break;}}return collection;
}

这里对 Lodash 的 forEach 进行了两个版本的优化:arrayEach 用于数组,baseEach 用于通用集合。它们都使用了一个 while 循环来迭代集合中的元素,调用传入的 iteratee 函数。

主要要点:

  1. 类型检测:forEach 函数中,通过 Array.isArray 来判断集合的类型。如果是数组,则使用 arrayEach 进行迭代;否则,使用 baseEach
  2. 迭代逻辑: arrayEachbaseEach 都使用了一个 while 循环来遍历集合。在每次迭代中,调用传入的 iteratee 函数,并检查其返回值。如果返回值为 false,则中断循环。
  3. 性能优化: Lodash 的实现对性能进行了一些优化,比如使用了 length 变量缓存集合的长度,减少了重复计算。
  4. 处理对象:baseEach 中,通过 Object(collection) 将集合转换为一个可迭代的对象。这样可以确保 baseEach 对于对象的处理更加一致,无论是否为数组。

那么为什么在特别大数据下会出现问题呢

使用 forEach 处理几万条数据量可能会导致栈溢出的问题,这通常是因为递归调用造成的。Lodash 的 forEach 实现是基于递归的,而不是基于循环的,因此对于大型数据集,递归深度可能会导致栈溢出。

为了解决这个问题,可以考虑使用其他方法,比如使用普通的 for 循环等。

http://www.lryc.cn/news/248463.html

相关文章:

  • Unity C++交互
  • 人工智能-优化算法之动量法
  • 【MySQL】InnoDB中的索引
  • 《软件工程原理与实践》复习总结与习题——软件工程
  • 软工2021上下午第六题(组合模式)
  • 在Spring Boot中使用不同的日志
  • 运维知识点-openResty
  • 微服务中配置Nacos热更新
  • ABAP2XLSX 的安装和demo
  • 记一篇Centos7安装innodb_ruby
  • VMware虚拟机安装和使用教程(附最新安装包+以ubuntu为例子讲解)
  • c语言 / 指针错误的几种情况
  • Stable-Diffusion——Windows部署教程
  • Day60.算法训练
  • 深入了解Java8新特性-日期时间API之TemporalQuery、TemporalQueries
  • 记录一次现网问题排查(分享查域名是否封禁小程序)
  • linux下实现Qt程序实现开机自启动
  • TCP 基本认识
  • 智慧城市包括哪些内容?有哪些智慧城市物联网方案?
  • Arkts@Watch装饰器与内置组件双向同步深度讲解与实战应用【鸿蒙专栏-14】
  • iMazing是什么软件?2024最新版本如何下载
  • LeetCode(40)同构字符串【哈希表】【简单】
  • 【代码随想录算法训练营-第一天】【数组】704. 二分查找、27. 移除元素
  • [教程] 一文进阶Redis
  • 通用plantuml模板头
  • 网站公安备案流程
  • 关于使用若依,并不会自动分页的解决方式
  • 在PyCharm中配置PyQt5环境
  • SIFI 极值点拟合的详细推导过程
  • Kontakt v7.7.2(音频采样器)