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;
    速度最快
    但是对于稍复杂点的数据来讲,速度并没有太大区别



    另:
    为什么简单的数据,消耗的时间反而更久???
  • 相关阅读:
    用sp_change_users_login消除Sql Server的孤立用户
    数据库连接字符串大全
    系统登录的设计与研究
    DB2常用命令大全(转)
    哈希表(HashTable)探究(转)
    转: C#实现的18位身份证格式验证算法
    通过SQLNET.ora文件限制Ip地址访问(转)
    AS/400(iSeries)
    使用Asp.Net构建安全网站
    DB2备份命名(转)
  • 原文地址:https://www.cnblogs.com/sixrookie/p/13634541.html
Copyright © 2011-2022 走看看