Editing data-bind cache in Knockout.js This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <div id="main" style="display: none" data-bind="template: 'repeat'"> </div> <script id="repeat" type="text/html"> < table > < tr data - bind = "repeat: { foreach: rows, item: '$row' }" > < td data - bind = "repeat: { foreach: $row().cells, item: '$cell', bind: 'text: \'Cell \' + $cell().id()'}" > < /td> </tr > < /table> </script> <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script> <script src="https://github.com/SteveSanderson/knockout/raw/master/build/output/knockout-latest.debug.js"> </script> <script type='text/javascript' src="https://raw.github.com/mbest/knockout-repeat/master/knockout-repeat.js"> </script> <script> function Cell(id) { this.id = ko.observable(id); } function Row(id) { this.id = ko.observable(id); this.cells = ko.observableArray(); } var viewModel = { rows: ko.observableArray() }; for (var i = 0; i < 1000; i++) { var row = new Row(i); for (var j = 0; j < 200; j++) { row.cells.push(new Cell(i + " - " + j)); } viewModel.rows.push(row); } ko.utils.buildEvalFunction = function(expression, scopeLevels) { // Build the source for a function that evaluates "expression" // For each scope variable, add an extra level of "with" nesting // Example result: with(sc[1]) { with(sc[0]) { return (expression) } } var functionBody = "return (" + expression + ")"; for (var i = 0; i < scopeLevels; i++) { functionBody = "with(sc[" + i + "]) { " + functionBody + " } "; } return new Function("sc", functionBody); }; ko.bindingProvider['instance'].bindingCache = {}; var withoutcache = ko.bindingProvider['instance'].parseBindingsString; var withcache = function(bindingsString, bindingContext) { try { var viewModel = bindingContext['$data']; var scopes = (typeof viewModel == 'object' && viewModel != null) ? [viewModel, bindingContext] : [bindingContext]; var cacheKey = scopes.length + '_' + bindingsString; var bindingFunction; if (cacheKey in this.bindingCache) { bindingFunction = this.bindingCache[cacheKey]; } else { var rewrittenBindings = " { " + ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson(bindingsString) + " } "; bindingFunction = this.bindingCache[cacheKey] = ko.utils.buildEvalFunction(rewrittenBindings, scopes.length); } return bindingFunction(scopes); } catch (ex) { throw new Error("Unable to parse bindings.\nMessage: " + ex + ";\nBindings value: " + bindingsString); } }; </script> Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) ko.utils.emptyDomNode($("#main")[0]); ko.bindingProvider['instance'].bindingCache = {}; Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code ko.bindingProvider['instance'].parseBindingsString = withcache; var main = $("#main"); ko.applyBindings(viewModel, main[0]); Test 2 Title Async (check if this is an asynchronous test) Code ko.bindingProvider['instance'].parseBindingsString = withoutcache; var main = $("#main"); ko.applyBindings(viewModel, main[0]);