Skip to content

Instantly share code, notes, and snippets.

@bromne
Last active May 31, 2018 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bromne/835985879871ce342f7624be9121a92f to your computer and use it in GitHub Desktop.
Save bromne/835985879871ce342f7624be9121a92f to your computer and use it in GitHub Desktop.
n × 2 and 2 × n
// 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