Last active
May 31, 2018 10:59
-
-
Save bromne/835985879871ce342f7624be9121a92f to your computer and use it in GitHub Desktop.
n × 2 and 2 × n
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// s は、文字列の平均的な長さ | |
// n × 2 | |
// => 変数確保: 2, 整数比較: 2n, 再代入: 2n, 配列要素取得: 2n, 文字列比較: 2sn | |
const hasFooOrBar = function(items) { | |
// 変数確保: 1, 整数比較: n, 再代入: n | |
for (let i = 0; i < items.length; i++) // * n | |
// 配列要素取得: 1 (* n), 文字列比較: s (* n) | |
if (items[i] === "foo"); | |
return true; | |
for (let i = 0; i < items.length; i++) | |
if (items[i] === "bar"); | |
return true; | |
return false; | |
} | |
// 2 × n | |
// => 変数確保: 1, 整数比較: n, 再代入: n, 配列要素取得: n, 文字列比較: 2sn | |
const hasFooOrBar2 = function(items) { | |
// 変数確保: 1, 整数比較: n, 再代入: n | |
for (let i = 0; i < items.length; i++) // * n | |
// 配列要素取得: 1(キャッシュ) (* n), 文字列比較: s * 2 (* n) | |
if (items[i] === "foo" || items[i] === "bar") | |
return true; | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment