Comments (17)
Hi @ZzZombo. Your performance issue sounds plausible. I'll be looking into it soon.
from jquery.initialize.
For reference: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
from jquery.initialize.
There are a few issues here (without delving into the code):
-
Subscription to all mutations. According to documentation, there are three mutation types:
childList
,attributes
, orcharacterData
. The script needs to observeattributes
for situations such as when theclass
orid
attributes are changed.childList
must be observed for when a new element is added to the document. We could probably ignorecharacterData
, so we can change that. -
The entire document is observed. There is the possibility to add extra options to limit this to specific elements, such as
<body>
or the main container<div>
, etc. This isn't a major issue anyhow. -
When a mutation is observed, we search the entire document for matching elements. This could be limited to the element where the mutation occured. However, this could break selectors that use child or descendant combinators. So, this would probably need to be an optional feature to ensure backward compatibility.
from jquery.initialize.
This shouldn't break anything. Nodes are already inserted/removed by the time an observer handles such actions. Same goes for all the other event types.
from jquery.initialize.
@ZzZombo I have 50+ selectors registered, and have no impacts on performance whatsoever. Are you sure that you're only initializing your selectors once? Because if they get reinitialized, let's say at partial loads on a dynamic page, that will choke the page in no time.
I know this from my own experience, because I have to do just that - detect newly inserted DOM elements at every partial ajax page load. The site went crazy when selectors were registered. I wrote a deInitialize func, removing either all or defined selectors, that I attached to a jQuery on hashchange callback. Works great!
@pie6k I'll fork your script and then I'll make a pull request, you are free to incorporate my added func as you see fit (if at all.)
from jquery.initialize.
The amount of selectors has no impact on performance whatsoever. It's the actual DOM that matters, whether it's a single element "Hello, world" page or both element heavy and dynamically changing one makes huge difference. Using this library in my userscript had actually made my browser ask me if I want to kill unresponsive page or not several times. I no longer use it, having learnt to use the MutationObserver API directly, and while the browser may choke sometimes on complex pages, it still yields better response times overall over the previous version of my script.
from jquery.initialize.
Edit: Deleted to avoid confusion. The content of this post is out-of-date.
from jquery.initialize.
The third feature has also now been implemented in #19.
from jquery.initialize.
Further improvements in #21.
@ZzZombo If you are interested in testing, please test the branch in #21, dbezborodovrp:feature/autoperf
To test via NPM:
npm install github:dbezborodovrp/jquery.initialize#feature/autoperf --save && npm install
from jquery.initialize.
You made the wrong call. Please read the API specs, or just do a simple test with debug output to browser console. When MutationObserver fires a callback, all changes in the batch it provides have ALREADY executed, as I have said it so in my first response 😩. That's why you are provided with the changed element and optionally **old**-whatever
attribute values and not the other way around, element to be changed, and **new**-something
values.
from jquery.initialize.
Have you seen and tested the changes?
I'm not sure I follow your reasoning. I know that the element has already been mutated before the observer sees it. That's not a problem. If fact, if the change were not visible, then it would be impossible to query the mutated nodes for matches. So, this seems completely irrelevant to me.
I may have not explained my changes correctly. Disregard the above. I will explain it afresh.
What I have done in #19 and #21 is to address the following concerns:
Does it mean that for all mutations
The plugin now ignores mutation types that are not childList
or attributes
it subscribed to to all elements in the document
By default, the plugin will observe all elements (that is, document.documentElement
. However, you can now override this by specifying an element. For example:
$.initialize.defaults.target = document.getElementById('observe-this-element');
Or:
$.initialize('div.foo', callbackFunction, { target: document.getElementById('observe-this-element') });
Furthermore, the plugin will not query the entire document for nodes matching the selector. It will only check the mutated node for a match (or its descendants or parents depending upon the complexity of the selector.)
from jquery.initialize.
Did you want to be able to set attributeOldValue
to true in the MutationObserverInit
options? I can expose that if that is what you are after.
from jquery.initialize.
$.initialize.defaults.scanDocument = false;
is not needed is all I say.
from jquery.initialize.
I agree. I removed that in #21.
from jquery.initialize.
Then great. Hope to use the updated version in a project soon.
from jquery.initialize.
Very good. The performance improvements haven't been released yet. If you want to test with your application, please do so by installing the test branch:
npm install github:dbezborodovrp/jquery.initialize#feature/autoperf --save && npm install
I also exposed observer options in 573fb61
from jquery.initialize.
These performance changes have been released under 1.2.0.
from jquery.initialize.
Related Issues (20)
- Site for Docs and Demo HOT 3
- Increment version on push! HOT 4
- Fails in FF 58 HOT 3
- unsuscribe from the initialize? HOT 6
- .forEach() doesn't work in old versions of chrome HOT 7
- Using a jquery selector: $('selector') HOT 1
- Looking for maintainers HOT 8
- Support jQuery selectors HOT 17
- Getting element `index` out of the `$.initialize` loop. HOT 4
- Remove initialize event HOT 3
- Not working HOT 2
- Include deprecated warning for old API HOT 3
- Initialize for multi clases is it possible ? HOT 7
- Dynamic loading execution order HOT 4
- Uncaught TypeError: Cannot read property 'querySelectorAll' of null HOT 2
- $.find.tokenize is missing in jQuery 3.7.0 HOT 5
- Refactor find/match algorithm
- Test Suite HOT 1
- No parentElement of html HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jquery.initialize.