Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

refactor($compile): use indexof/splice instead of manually manipulating jquery object#11248

Closed
jbedard wants to merge 1 commit into
angular:masterfrom
jbedard:compile-replacewith-simplify
Closed

refactor($compile): use indexof/splice instead of manually manipulating jquery object#11248
jbedard wants to merge 1 commit into
angular:masterfrom
jbedard:compile-replacewith-simplify

Conversation

@jbedard

@jbedard jbedard commented Mar 5, 2015

Copy link
Copy Markdown
Contributor

jQuery/jqLite objects have a splice method on them, and indexof.call works on them, so those 2 methods can be used instead of the double loop.

The standard case (replacing one element) just does an assignment while other cases use splice. This could be changed to always use splice if wanted, but I thought the 99% case was worth it.

(this is a subset of #9365 that can be done on it's own)

@jbedard jbedard force-pushed the compile-replacewith-simplify branch from d3984a4 to eee7f45 Compare March 5, 2015 09:39
@Narretz Narretz added this to the Backlog milestone Mar 5, 2015
@lgalfaso

Copy link
Copy Markdown
Contributor

This looks interesting, would it be possible to get some numbers to know if there are any performance implications?

@jbedard

jbedard commented Mar 12, 2015

Copy link
Copy Markdown
Contributor Author

Seems to make no difference when running $compile("<div ng-if='true'></div>") in a loop. Although if I take out the if (removeCount === 1) case it is a couple percent slower.

There are other things in this method that can be changed if we want to speed it up though (see #9365).

@lgalfaso

Copy link
Copy Markdown
Contributor

@jbedard can you also try the performance on the case where multiple elements would need to be replaced?

What remains at #9365, I think it would be best if they can be split into more PRs (it you think that only one PR would be needed, then just rebase it once this lands)

@jbedard

jbedard commented Mar 13, 2015

Copy link
Copy Markdown
Contributor Author

Multiple elements seems to have the same result as removing the if (removeCount === 1) case for single elements - it is a couple percent slower and a fraction of a percent more memory (because splice returns a new array, I assume).

If you profile only the code changed on its own (indexOf+splice vs the loop) the loop one is 1-2x faster. But compared to the document fragment and data calls in this method this is insignificant.

Think it's worth removing that loop?

@lgalfaso

Copy link
Copy Markdown
Contributor

I would leave it as it... maybe just

diff --git a/src/ng/compile.js b/src/ng/compile.js
index 3de0d53..f582206 100644
--- a/src/ng/compile.js
+++ b/src/ng/compile.js
@@ -2424,12 +2424,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
             $rootElement[i++] = newNode;
             for (var j = i, j2 = j + removeCount - 1,
                      jj = $rootElement.length;
-                 j < jj; j++, j2++) {
-              if (j2 < jj) {
-                $rootElement[j] = $rootElement[j2];
-              } else {
-                delete $rootElement[j];
-              }
+                 j2 < jj; j++, j2++) {
+              $rootElement[j] = $rootElement[j2];
             }
             $rootElement.length -= removeCount - 1;

@jbedard jbedard force-pushed the compile-replacewith-simplify branch from eee7f45 to e57a7a6 Compare June 6, 2015 17:15
@jbedard

jbedard commented Nov 6, 2015

Copy link
Copy Markdown
Contributor Author

I still think this is worth removing that ugly loop, but feel free to close it if you disagree. Note that your snippet above won't work (I think...) because these are (always? often?) jqLite objects and not arrays so the delete is needed in addition to the length change...

@jbedard jbedard force-pushed the compile-replacewith-simplify branch 2 times, most recently from fd40778 to 174ee7d Compare August 27, 2016 04:29
@jbedard jbedard force-pushed the compile-replacewith-simplify branch from 174ee7d to 2eefe51 Compare August 27, 2016 10:56
@mgol

mgol commented Mar 29, 2018

Copy link
Copy Markdown
Member

I'm going through old jqLite-related issues/PRs. What's the state of this one? Is it still worth landing, @jbedard?

@jbedard

jbedard commented Mar 29, 2018

Copy link
Copy Markdown
Contributor Author

I still think this makes things prettier, but others seemed to disagree. I'm fine closing it...

@jbedard

jbedard commented May 13, 2018

Copy link
Copy Markdown
Contributor Author

Looks like this won't happen, closing.

@jbedard jbedard closed this May 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants