首页 > 其他分享 >[V8] Object & array copying

[V8] Object & array copying

时间:2023-11-13 15:34:51浏览次数:37  
标签:obj sum Object timer V8 const array copy

import { createBenchmark } from "./benchmark";

class MyArray extends Array {}

const SIZE = 100;
const obj: Record<string, number> = {};
/**
 *  {
 *  _0: 0,
 *  _1: 1,
 *  _2: 2,
 *  ...
 * }
 */
const array = [];
/**
 *  [
 *  '_0', 0,
 *  '_1', 1,
 *  '_2', 2,
 *  ...
 * }
 */

for (let i = 0; i < SIZE; i++) {
  obj["_" + i] = i;
  array.push("_" + i, i);
}

(function doBenchmarks(benchmark) {
  (function benchmark1(obj, timer) {
    let sum = 0;
    while (timer()) {
      for (const key in obj) {
        sum += obj[key];
      }
    }
  })(obj, benchmark("property"));

  (function benchmark1(obj, timer) {
    let sum = 0;
    while (timer()) {
      const copy = { ...obj };
      for (const key in copy) {
        sum += copy[key];
      }
    }
  })(obj, benchmark("property with copy"));

  (function benchmark2(array, timer) {
    let sum = 0;
    while (timer()) {
      for (let i = 0; i < array.length; i += 2) {
        sum += array[i] as number;
      }
    }
  })(array, benchmark("array"));

  (function benchmark2(array, timer) {
    let sum = 0;
    while (timer()) {
      const copy = array.slice();
      for (let i = 0; i < copy.length; i += 2) {
        sum += copy[i] as number;
      }
    }
  })(array, benchmark("array with copy"));

  benchmark.report();
})(createBenchmark("iteration"));

From the result we can see that, using array.slice()to copy a array is quite light weight operation, it doesn't affect performance that much.

But using {...obj}is expensive, it affect performance by a lot.

It is due to the hidden class of the object, everytime you push a new prop into object it will create a new hidden class.

标签:obj,sum,Object,timer,V8,const,array,copy
From: https://www.cnblogs.com/Answer1215/p/17829250.html

相关文章

  • 【刷题笔记】108. Convert Sorted Array to Binary Search Tree
    题目Givenanarraywhereelementsaresortedinascendingorder,convertittoaheightbalancedBST.Forthisproblem,aheight-balancedbinarytreeisdefinedasabinarytreeinwhichthedepthofthetwosubtreesof every nodeneverdifferbymorethan......
  • Springboot内置的工具类之ObjectUtils
    在实际业务开发中,有时候经常需要判断对象是否为空、数组是否为空、两个对象是否相等,数组中是否包含某个元素,往数组中追加元素等这些操作,每次都手写太麻烦,然后很多人的选择是封装成util工具类,实际上类似这些东西,如果项目使用了spring的框架,根本不需要封装,org.springframework.util.O......
  • Java 集合—ArrayList
    ArrayList介绍ArrayList 的底层是数组队列,相当于动态数组。与Java中的数组相比,它的容量能动态增长。ArrayList类位于java.util包中,使用前需要引入它,语法格式如下:importjava.util.ArrayList;//引入ArrayList类ArrayList<E>objectName=newArrayList<>();//初始化......
  • python rpy2.robjects库总结
    参考:https://rpy2.github.io/介绍rpy2是用C语言编写的,是嵌入在Python进程中的R运行接口。该包由几个子包或模块组成:1)rpy2.rinterface:R的低级接口,当速度和灵活性最重要的时候。接近R的c级API。2)rpy2.robjects:R的高级接口,注重易用性。更友好,使用更广泛。3)rpy2.interactive......
  • 格式转换:相机帧void* pBuffer,QImage,cv::Mat,Halconcpp::HObject
    【说明】1、若传递的是指针,则内存共享,其一改变,另一个也被改变。为了避免输入被更改,做了些处理。如QImage2Mat中使用了两个变量mat,out。2、有的存在宽度方向4字节对齐情况,所以做了些处理。如QImage2HObject中让宽度变为4的整数倍。 【相机帧void*pBuffer赋给其他格式】 ......
  • java ArrayList的基本使用
    packagecom.elaina.test1;importjava.util.ArrayList;publicclasstest1{publicstaticvoidmain(String[]args){//1.创建集合的对象//泛型:限定集合中的存储数据的类型//ArrayList<String>list=newArrayList<String>();......
  • [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}......
  • 对象定义 Object.create Object.defineProperty
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-......
  • webgoat _v8.1全流程通关
    1.1(A1)SQLInjection(intro)概念 本课程介绍了什么是结构化查询语言(SQL),以及如何操纵它以执行开发人员原始意图之外的任务。 目标 用户将对SQL的工作原理和用途有基本的了解。 用户将对SQL注入有基本的了解,以及它们的工作原理。 用户将展示对以下内容的知识: ......