zoukankan      html  css  js  c++  java
  • JS

    • pop()push()等在数组尾部操作的方法的时间复杂度为O(1)
    • forEach()map()shift()unshift()、等需要遍历或者在数组头部操作的方法的时间复杂度为O(n)
    • splice()concat()find()等方法的时间时间复杂度为O(n),但最优情况可能为O(1),如splice()在数组尾部操作、find()第一个元素就符合条件。

    不是十分确定的解释:因为 JS 数组是用 HashTable 实现的(上网查资料至少 V8 引擎中是),在数组中间进行操作需要将操作的元素后面的元素的 hash 值(keyindex 经过 hash function 计算出来的值)改变,要是在数组头部进行增加和删除就需要将数组全部元素对应的 hash 值(keyindex 经过 hash function 计算出来的值)改变。

    测试:

    var array = [];
    for(var i = 0;i< 1000000;i++){
        array.push(i)
    }
    var start = new Date().getTime()
    for(var i = 0; i< 100000; i++){
        array.splice(1000000,0,1);
    }
    var end = new Date().getTime();
    console.log(`Add 10^5 numbers to the head of array: ${end - start} ms`);
    
    var array = [];
    for(var i = 0;i< 1000000;i++){
        array.push(i)
    }
    var start = new Date().getTime()
    for(var i = 0; i< 100000; i++){
        array.push(1000000,0,1);
    }
    var end = new Date().getTime();
    console.log(`Add 10^5 numbers to the rear of array: ${end - start} ms`);
    
    // Add 10^5 numbers to the head of array: 4555 ms
    // Add 10^5 numbers to the rear of array: 56 ms
    
  • 相关阅读:
    如何理解css3 -webkit-animation-fill-mode属性值为both时的使用方法
    关于对canvas.beginPath()的理解
    [cf10E]Greedy Change
    [atAGC055B]ABC Supremacy
    [loj6734]图上的游戏
    [gym102412D]The Jump from Height of Self-importance to Height of IQ Level
    [Aizu1410]Draw in Straight Lines
    [Aizu2993]Invariant Tree
    [zoj3990]Tree Equation
    [hdu6326]Monster Hunter
  • 原文地址:https://www.cnblogs.com/jffun-blog/p/11674568.html
Copyright © 2011-2022 走看看