Updates contribution guidelines for JS object performance
In a new section, titled performance, a requirement is added for JavaScript code to limit the re-creation of objects in the same scope. An example with jQuery is given with explanation. Change-Id: I2f0d8392277f65384389d8d20154d645197050af Partial-Bug: #1412971
This commit is contained in:
parent
b4091382c9
commit
52f401457f
|
@ -309,6 +309,28 @@ Required
|
||||||
* Avoid commented-out code.
|
* Avoid commented-out code.
|
||||||
* Avoid dead code.
|
* Avoid dead code.
|
||||||
|
|
||||||
|
**Performance**
|
||||||
|
|
||||||
|
* Avoid creating instances of the same object repeatedly within the same scope.
|
||||||
|
Instead, assign the object to a variable and re-use the existing object. For
|
||||||
|
example:
|
||||||
|
|
||||||
|
.. code ::
|
||||||
|
|
||||||
|
$(document).on('click', function() { /* do something. */ });
|
||||||
|
$(document).on('mouseover', function() { /* do something. */ });
|
||||||
|
|
||||||
|
A better approach:
|
||||||
|
|
||||||
|
.. code ::
|
||||||
|
|
||||||
|
var $document = $(document);
|
||||||
|
$document.on('click', function() { /* do something. */ });
|
||||||
|
$document.on('mouseover', function() { /* do something. */ });
|
||||||
|
|
||||||
|
In the first approach a jQuery object for ``document`` is created each time.
|
||||||
|
The second approach creates only one jQuery object and reuses it. Each object
|
||||||
|
needs to be created, uses memory, and needs to be garbage collected.
|
||||||
|
|
||||||
Recommended
|
Recommended
|
||||||
~~~~~~~~~~~
|
~~~~~~~~~~~
|
||||||
|
|
Loading…
Reference in New Issue