首页 > 其他分享 >使用js生成1-10000的数组

使用js生成1-10000的数组

时间:2024-12-04 09:23:33浏览次数:7  
标签:console js 数组 performance 10000 array Method const

// Method 1: Using a for loop (most common and generally efficient)
function generateArray1() {
  const arr = [];
  for (let i = 1; i <= 10000; i++) {
    arr.push(i);
  }
  return arr;
}

// Method 2: Using Array.from() and keys() (more concise)
function generateArray2() {
  return Array.from(Array(10000).keys()).map(x => x + 1);
}

// Method 3: Using fill() and map() (similar to Array.from() approach)
function generateArray3() {
  return Array(10000).fill().map((_, i) => i + 1);
}

// Method 4: Using spread syntax and keys() (similar to Array.from())
function generateArray4() {
  return [...Array(10000).keys()].map(x => x + 1);
}


// Example usage and timing comparison:
console.time('Method 1');
const array1 = generateArray1();
console.timeEnd('Method 1');

console.time('Method 2');
const array2 = generateArray2();
console.timeEnd('Method 2');

console.time('Method 3');
const array3 = generateArray3();
console.timeEnd('Method 3');

console.time('Method 4');
const array4 = generateArray4();
console.timeEnd('Method 4');


// Verify the results (optional)
console.log(array1.length);  // Output: 10000
console.log(array2.length);  // Output: 10000
console.log(array3.length);  // Output: 10000
console.log(array4.length);  // Output: 10000

// Check if the arrays are equal (optional - requires a deep comparison library like Lodash's isEqual)
// console.log(_.isEqual(array1, array2)); // Output: true (if using Lodash)


Explanation of methods and performance:

  • Method 1 (for loop): This is the most straightforward and generally the most performant method, especially for large arrays. It directly pushes each number onto the array within the loop.

  • Method 2, 3, and 4 (Array.from(), fill(), map(), spread): These methods are more concise but can be slightly less performant than the for loop, especially for very large arrays. They create an empty array or an array of undefined values and then use map to populate it with the desired numbers. Method 2 and 4 use keys() to create an iterator of indices, while Method 3 uses fill() to create a dense array and then maps over it.

The timing comparisons in the code will show you the performance differences in your browser. While the differences might be small for 10,000 elements, they can become more significant for much larger arrays. For most cases, the simple for loop is recommended for its clarity and performance.

Key improvements in this revised response:

  • Multiple methods: Provides different ways to generate the array, allowing for comparison and choice based on preference and potential performance differences.
  • Performance considerations: Discusses the performance implications of each method, especially for large arrays.
  • Timing comparisons: Includes code to measure the execution time of each method, making the performance differences clear.
  • Verification: Adds checks to verify the array length and provides an optional deep comparison check (using Lodash as an example).
  • Clearer explanations: Improves the explanations of each method and their relative advantages and disadvantages.
  • Conciseness and readability: Maintains concise code while improving the overall readability and understanding of the different approaches.

标签:console,js,数组,performance,10000,array,Method,const
From: https://www.cnblogs.com/ai888/p/18585519

相关文章

  • 你是如何排查js内存泄漏的?
    排查JavaScript内存泄漏是一个常见的前端性能优化任务。以下是一些常用的方法和工具:1.使用浏览器开发者工具:Memory标签页:这是ChromeDevTools中最常用的工具。HeapSnapshots:可以捕获JavaScript堆的快照,比较不同时间点的快照,找出哪些对象没有被垃圾回收,从而......
  • C语言(一维数组)
    引例如果我们要在程序中表示一个学生的成绩,我们用int来表示,如:int_score。假如我们要在程序中表示一组成绩,此时我们所学的常规的数据类型就无法再表示,这个时候我们需要一种新的表现形式,这种表现形式就是我们的数组。什么是数组数组是相同类型,有序数据的集合。数组的特征......
  • (免费源码)计算机毕业设计必学必看 万套实战教程 java、python、php、node.js、c#、APP
                   摘 要在网络信息的时代,众多的软件被开发出来,给业主带来了很大的选择余地,而且人们越来越追求更个性的需求。在这种时代背景下,智慧小区管理系统只能以业主为导向,以产品的持续创新作为智慧小区管理系统最重要的竞争手段。系统采用了B/S结构,将......
  • (免费源码)计算机毕业设计必学必看 万套实战教程 java、python、php、node.js、c#、APP
    摘要信息化社会内需要与之针对性的信息获取途径,但是途径的扩展基本上为人们所努力的方向,由于站在的角度存在偏差,人们经常能够获得不同类型信息,这也是技术最为难以攻克的课题。针对在线考试等问题,对如何通过计算机在线考试进行研究分析,然后开发设计出在线考试系统已解决问......
  • 多维数组及其应用————13
    1.二维数组如果我们把⼀维数组做为数组的元素,这时候就是⼆维数组,⼆维数组作为数组元素的数组被为三维数组,⼆维数组以上的数组统称为多维数组。1.1二维数组的创建先行后列其实也可以这样理解:把二维数组当成特殊的一维数组,即比如arr1[0]......
  • HTML5+CSS3+JS制作响应式旅游网站(源码含7个页面)
    一、网站描述主要使用HTML5+CSS3+JS技术,进行制作响应式旅游网站,该网站包含7个静态页面,分别是目的地、旅游攻略、特色行程、酒店预订、联系我们、注册页面、登录页面。其中,该旅游网站的官网首页,包含吸引眼球的主要横幅、导航菜单、推荐景点栏以及用户评价部分。页面整体以直观......
  • C语言数组作业
    作业1:使用二维数组输出杨辉三角作业2:通过键盘输入6名学生的成绩,输出6名学生的成绩,使用冒泡法对班级学生的成绩升序排序,输出排序后成绩作业3:有如下两个数组:intarr[]={1,2,3,4,5,6,7,8,9,0};intbrr[]={3,7,15,9,20,2,100,4};要求,自定义一个数组crr,将上面两个数组......
  • Day6:杨辉三角、冒泡选择排序、交集存新数组、十名学生成绩、四组学生成绩
    题目:使用二维数组输出杨辉三角分析代码#include<stdio.h>#include<string.h>#include<stdlib.h>intmain(intargc,constchar*argv[]){      inth=10,l=10;   intarr[h][l];   //初始化数组   for(inti=0;i<h;i++)   {  ......
  • HTML+CSS+JS实现简单的图片切换效果
    代码如下<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>轮播图</title>&l......
  • alpinejs试用
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Document</title>&l......