首页 > 其他分享 >[V8] Holey Arrays

[V8] Holey Arrays

时间:2023-11-13 16:13:18浏览次数:32  
标签:const holey Holey Arrays sum value V8 array Array

What is holey array: an array with hole(s)

const array = [1,2, ,3]

Why this is a problem? Should array[2] to be undefined?

Yes and no.. normally it is undefined, but before that, VM has been check Array.prototypeto see Array.prototype["2"]whether it has value assigned or not. If not then VM set it to undefined, otherwise not.

(() => {
  const array = [1, 2, , 3];

  let sum = 0;
  for (let i = 0; i < array.length; i++) {
    const value = array[i];
    if (typeof value == "number") {
      sum += value;
    }
  }
  console.log("Sum:", sum); // 6
})();

 

Now try to mass up Array.prototype:

(() => {
  const array = [1, 2, , 3];
  Array.prototype["2"] = -3; // <-- this line

  let sum = 0;
  for (let i = 0; i < array.length; i++) {
    const value = array[i];
    if (typeof value == "number") {
      sum += value;
    }
  }
  console.log("Sum:", sum); // 3
})();

 

Is there any other way to create a holey array?

Yes: Array.from(10), once you create a holey array, then all the rest operation .map, .filter, .reducewill also build based on this holey array.

 

How to prevent that?

Array.from(10, (_el, _i) => 0) // give a defualt value when init the array

标签:const,holey,Holey,Arrays,sum,value,V8,array,Array
From: https://www.cnblogs.com/Answer1215/p/17829373.html

相关文章

  • [V8] Object & array copying
    import{createBenchmark}from"./benchmark";classMyArrayextendsArray{}constSIZE=100;constobj:Record<string,number>={};/***{*_0:0,*_1:1,*_2:2,*...*}*/constarray=[];/***[*'_0&......
  • [V8] Double & Triple Equals
    doubleequalsis15timesslowerthantripleequals.Underhooddoubleequalsneedtocall valueOf()functiontoconvertthevalue.({valueOf:()=>3})==3//true({valueOf:()=>3})===3//false ......
  • [V8] Object Shapes & Inline Caching
    Benchmark:查看代码import{createBenchmark}from'./benchmark';constARRAY_SIZE=10000;constarray1=[];//{value,prop_0},{value,prop_0},{value,prop_0},{value,prop_0},constarray2=[];//{value,prop_0},{value,prop_1},{value,prop_0}......
  • webgoat _v8.1全流程通关
    1.1(A1)SQLInjection(intro)概念 本课程介绍了什么是结构化查询语言(SQL),以及如何操纵它以执行开发人员原始意图之外的任务。 目标 用户将对SQL的工作原理和用途有基本的了解。 用户将对SQL注入有基本的了解,以及它们的工作原理。 用户将展示对以下内容的知识: ......
  • C. Serval and Toxel's Arrays 组合数学
    题目链接......
  • 线程安全集合(JDK1.5之前和之后)、CopyOnWriteArrayList、CopyOnWriteArraySet
    JDK1.5之前JDK1.5之前:Collections.synchronizedList JDK1.5之后CopyOnWriteArrayList   CopyOnWriteArraySet    ......
  • 关于Java使用Arrays类的equals()函数比较两个数组是否相等功能的实战
    关键词:文件流问题:二进制流文件丢失解决方法:java.util.Arrays.equals(byte1[],byte2[])分析:Arrays.equals()函数比较的是数组的内容而不是引用。也就是说,只有数组的元素内容相同,并且顺序也相同,才会返回true。      如果数组的元素内容相同但顺序不同,或者数组的引用......
  • FS2957 降压恒压芯片内置120V功率管36V48V60V72V80v降压5V
    随着科技的不断进步,电子设备在我们的生活中越来越普及,而电源管理芯片作为电子设备中的重要组成部分,也得到了广泛的应用。今天,我们要介绍的是一款具有高性价比的FS2957降压恒压芯片,它内置120V功率管,适用于36V、48V、60V、72V、80V的降压5V输出。FS2957降压恒压芯片采用专利的电流模......
  • YOLOv8运行参数解读
    从YAML中构建一个新模型,并从头开始训练yolodetecttraindata=coco128.yamlmodel=yolov8n.yamlepochs=100imgsz=640#从预先训练的*.pt模型开始训练yolodetecttraindata=coco128.yamlmodel=yolov8n.ptepochs=100imgsz=640#从YAML中构建一个新的模型,将预训练的权......
  • YOLOv8上手——随手录
    YOLO81.配置环境pytorch环境以及cudn环境就不详解,只需要根据pytorch官网安装后再pipopencv即可正常运行2.gityolov8(ps:现在也可以直接通过pip下载yolo8了)运行起来可以看官网给出的文档,里面有许多案例以及参数的含义,一下内容都是根据博主个人常用的,不够详细敬请谅解from......