zoukankan      html  css  js  c++  java
  • 对象删除(消耗时间验证)

    删除对象有两种方法:

    1、delete 2、赋值undefined

    简单对象:

    let data = {a:1,b:2,c:3,d:4}
    
    fn1(){
      console.time('消耗时间');
      data['d'] = undefined;
      console.timeEnd('消耗时间');
    }
    fn2(){
      console.time('消耗时间');
      data.d = undefined;
      console.timeEnd('消耗时间');
    }
    fn3(){
      console.time('消耗时间');
      delete data.d;
      console.timeEnd('消耗时间');
    }
    
    fn1();
    消耗时间: 0.013671875ms
    
    fn2();
    消耗时间: 0.0029296875ms
    
    fn3();
    消耗时间: 0.004150390625ms

    稍复杂点的对象:

    let data = {
        "ircEvent": "PRIVMSG",
        "method": "newURI",
        "regex": "^http://.*"
    }
    function fn1(){
      console.time('消耗时间');
      data['regex'] = undefined;
      console.timeEnd('消耗时间');
    }
    function fn2(){
      console.time('消耗时间');
      data.regex = undefined;
      console.timeEnd('消耗时间');
    }
    function fn3(){
      console.time('消耗时间');
      delete data.regex;
      console.timeEnd('消耗时间');
    }
    
    fn1()
     消耗时间: 0.003173828125ms
    
    fn2()
    消耗时间: 0.003173828125ms
    
    fn3()
     消耗时间: 0.003662109375ms

    从执行时间上来看,对于特别简单的对象使用:

    data.d = undefined;
    速度最快
    但是对于稍复杂点的数据来讲,速度并没有太大区别



    另:
    为什么简单的数据,消耗的时间反而更久???
  • 相关阅读:
    使用gitblit搭建一个简单的局域网服务器
    Git的一些基本操作和命令
    oracle 常用函数
    Windows 下用 gogs 配置局域网 git server
    慎用 new、delete
    探究functools模块wraps装饰器的用途
    处理QMenu的triggered信号时遇到的一个问题
    浅析MySQL中exists与in的使用
    java-工具类-读取配置文件
    Java web的基本概念
  • 原文地址:https://www.cnblogs.com/sixrookie/p/13634541.html
Copyright © 2011-2022 走看看