javascript Array.prototype.filter()的时间复杂度?

时间:2016-11-06 07:38:33

标签: javascript

Array.protoype.filter的大O是什么?

我查看了文档(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter),但还没有能够解决它。

1 个答案:

答案 0 :(得分:2)

O(N)

示例:

var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => isBig(word));

function isBig(word){
  	console.log("called");
	return word.length > 6;
}

输出:

  

“称为”   “叫”   “叫”   “叫”   “叫”   “叫”   数组[“旺盛”,“破坏”,“现在”]