Skip to content

javascript_in_depth_11

paul edited this page Apr 20, 2017 · 1 revision

๋””์ž์ธ ํŒจํ„ด ์‹ค์šฉ - ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ํŒจํ„ด

๋ฉ”๋ชจ์ด์ œ์ด์…˜ ํŒจํ„ด์€ ์ด์ „์— ๊ณ„์‚ฐํ•œ ๊ฐ’์„ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•จ์œผ๋กœ์จ ๋™์ผํ•œ ๊ณ„์‚ฐ์˜ ๋ฐ˜๋ณต ์ˆ˜ํ–‰์„ ์ œ๊ฑฐํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์†๋„๋ฅผ ๋น ๋ฅด๊ฒŒ ํ•˜๋Š” ๊ธฐ์ˆ ์ด๋‹ค. ์ด๋ฆ„๋Œ€๋กœ '๋ฉ”๋ชจ'๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ํŠน์ง•์ธ๋ฐ, ์ด ๋ฉ”๋ชจ๋ฅผ ํ•˜๋Š” ๋Œ€์ƒ์€ ํ•จ์ˆ˜ ๋˜๋Š” ๊ฐ์ฒด์ด๋‹ค.

ํ™œ์šฉ

์ผ๋ฐ˜์ ์ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํŠนํžˆ ์ปดํ“จํŒ… ์ž์›์ด ์†Œ๋ชจ๋˜๊ฑฐ๋‚˜ ์ฒ˜๋ฆฌ ์‹œ๊ฐ„์ด ๊ธด ์‚ฐ์ˆ ์‹์— ๋Œ€ํ•˜์—ฌ ์บ์‹œ๋ฅผ ํ•ด๋‘๋Š” ์‹์œผ๋กœ ํ™œ์šฉํ•˜๋ฉด ์ข‹๋‹ค. ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ํŒจํ„ด์€ ๊ณ„์‚ฐ์ด๋‚˜ ์š”์ฒญ์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋ฉด์„œ ๋ฐ˜๋ณต์ ์œผ๋กœ ๊ฐ™์€ ํ˜ธ์ถœ์ด ์ผ์–ด๋‚  ๋•Œ๋‚˜ ์žฌ๊ท€ ํ•จ์ˆ˜ ๊ตฌํ˜„์‹œ์— ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ํŒจํ„ด์„ ์„ค๊ณ„ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ค€๋‹ค.

์˜ˆ์ œ

// ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ํŒจํ„ด์„ ์ด์šฉํ•œ ์บ์‹œ ๊ตฌํ˜„
(function () {
    var inputItemId = document.getElementById("itemId");
    function searchItem(id) {
        var xhr;
        if (searchItem.cache.hasOwnProperty(id)) {
            return searchItem.cache[id];
        }
        xhr = new XMLHttpRequest();
        xhr.open("GET", "/searchItem");
        xhr.onload = function () {
            var item = JSON.parse(xhr.responseText);
            searchItem.cache[item.id] = item;
        }
        xhr.send();
    }
    searchItem.cache = {};
    document.getElementById("search").addEventListener("click", function () {
        searchItem(searchItem.value);
    });
}());

Function.prototype ํ™œ์šฉ

๋ฉ”๋ชจ์ด์ œ์ด์…˜ ํŒจํ„ด์„ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก Function์˜ ํ”„๋กœํ† ํƒ€์ž…์— ์ถ”๊ฐ€ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋ณต์žกํ•˜์ง€ ์•Š๊ณ  ํŽธ๋ฆฌํ•˜๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

// Function ๊ธฐ๋ณธ ํ•จ์ˆ˜๋กœ memoize ํ•จ์ˆ˜ ์ •์˜
(function () {
    Function.prototype.memoize = function () {
        var _this = this,
            memo = {};
        return function () {
            var argsString = JSON.stringify(arguments),
                returnValue;
            if (memo.hasOwnProperty(argsString)) {
                return memo[argsString];
            } else {
                returnValue = _this.apply(this, arguments);
                memo[argsString] = returnValue;
                return returnValue;
            }
        }
    };

    function fibonacci(n) {
        if (n === 0 || n === 1) {
            return n;
        } else {
            return fibonacci(n - 1) + fibonacci(n - 2);
        }
    }

    var fibonacciMemo = fibonacci.memoize();

    var testNum = 10, start, end;
    start = Date.now();
    console.log(fibonacciMemo(testNum));
    end = Date.now();
    console.log(`Elapsed time of ${((end-start)/1000).toFixed(2)} seconds for recursive fibonacciMemo(${testNum}) for the first time`);


    start = Date.now();
    console.log(fibonacciMemo(testNum));
    end = Date.now();
    console.log(`Elapsed time of ${((end-start)/1000).toFixed(2)} seconds for recursive fibonacciMemo(${testNum}) for the second time`);
}());

Clone this wiki locally