Earlier we have written a post KendoUI's slowest function and now, we want to point to the next slow function, which is kendo.guid(). It's used to assign uid to each observable object, and also in couple of other places.
kendo.guid()
uid
Here is its source:
guid: function() { var id = "", i, random; for (i = 0; i < 32; i++) { random = math.random() * 16 | 0; if (i == 8 || i == 12 || i == 16 || i == 20) { id += "-"; } id += (i == 12 ? 4 : (i == 16 ? (random & 3 | 8) : random)).toString(16); } return id; }
KendoUI people have decided to define uid as a string in format of Globally unique identifier. We think there is no reason to have such a complex value; it's enough to have counter to generate uid values. As KendoUI relies on the string type of uid, so we have defined a patch like this:
var guid = 0 kendo.guid = function() { return ++guid + "" }
Consider now a test case. It's almost identical to that in previous post:
<!DOCTYPE html> <html> <head> <title>Test</title> <script src="scripts/jquery/jquery.min..js"></script> <script src="scripts/kendo/kendo.web.min.js"></script> <link href="styles/kendo.common.min.css" rel="stylesheet" /> <link href="styles/kendo.default.min.css" rel="stylesheet" /> <link href="styles/style.css" rel="stylesheet" /> <script> var model; function init() { var source = []; for(var i = 0; i < 1000; ++i) { source.push({ text: "value " + i, value: "" + i }); } model = kendo.observable( { value: "1", source: new kendo.data.DataSource( { data: source }) }); model.source.read(); } function patch() { var base = kendo.data.binders.widget.source.fn._ns; var result; var guid = 0; kendo.guid = function() { return ++guid + ""; }; kendo.data.binders.widget.source.fn._ns = function(ns) { return ns ? base.call(this, ns) : (result || (result = base.call(this, ns))); } } function test() { init(); kendo.bind("#view", model); } patch(); </script> </head> <body> <p> <button onclick="test()">Click to start test</button> </p> <p id="view"> Select: <input data-role="dropdownlist" data-bind="value: value, source: source" data-text-field="text" data-value-field="value"/> </p> </body> </html>
Now, we can compare performance with and without that patch.
Here is a run statistics without patch:
and with patch:
Note that statistics were collected for IE 10. An example can be found at slow2.html.