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

设置网站文件夹的安全项搜索引擎营销优化诊断训练

设置网站文件夹的安全项,搜索引擎营销优化诊断训练,企业移动网站建设,河南建设监理协会新网站什么是lodash Lodash 是一个 JavaScript 实用工具库,提供了很多用于处理数据、简化开发等方面的功能。它提供了一组常用的工具函数,用于处理数组、对象、字符串等常见数据结构,同时也包含了一些函数式编程的工具。对于前端开发来说&#xff…

什么是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.hkea.cn/news/567014/

相关文章:

  • 丰县住房与城乡建设部网站太原网站制作优化seo公司
  • 微信如何做微商城网站建设手机网站智能建站
  • 网站尾部分页数字怎么做推广app大全
  • 建筑设计软件有哪些优化网站建设
  • 网站开发 word文件预览医疗器械龙头股
  • 电子商务网站建设花费南宁百度seo排名价格
  • 做公司网站要注意哪些问题真正免费建站网站
  • 在线服务器代理杭州seo网络公司
  • wordpress邮件订阅seo技术外包
  • 深圳营销网站建站公司搜索引擎关键词的工具
  • 做网站如何网站考虑优化游戏推广员是诈骗吗
  • 公众号做视频网站吗关键词排名怎么做上首页
  • 重庆做网站价格优化软件下载
  • 如何做网站镜像今日最火的新闻
  • 水果网站开发所需的成本市场营销实际案例
  • 无锡市新吴区住房和建设交通局网站西安百度关键词包年
  • 网站平台方案设计seo上首页
  • 郑州做网站的联系方式搜狗友链交换
  • 一般建设一个网站多少钱怎么接广告赚钱
  • 计算机专业网站开发方向销售推广方案
  • 上海网站建设公司排名西安百度公司
  • 中国网网址是多少网站推广优化教程
  • 关于加强机关网站建设运营培训
  • dw做的网站怎么让别人看到如何建立一个网站
  • 保险网站建设优缺点seo代码优化步骤
  • 如何快速建网站百度电脑版入口
  • 山东省建设工程信息网站最近最新的新闻
  • 免费网站建设方案锦绣大地seo官网
  • 电子商务的网站建设牛排seo系统
  • 资源收费网站怎么做网站快速优化排名官网