GithubHelp home page GithubHelp logo

chuanshuoye / mutationobserver.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from megawac/mutationobserver.js

0.0 1.0 0.0 477 KB

ES3 Shim for the MutationObserver interface via DOM diffing.

License: MIT License

JavaScript 98.59% HTML 1.41%

mutationobserver.js's Introduction

MutationObserver

Browser Test Status
Note: the svg swapped the working browsers; IE8 works while IE7 fails 1 test

A polyfill for the MutationObserver API (can I use?). The polyfill is more cause we can than should (with subtree at any rate)... It's async and uses a recursive timeout fallback (default checks changes every 30ms + runtime) instead of using the deprecated DOM3 MutationEvents so theoretically can support virtually any environment.

$ npm install mutationobserver-shim
$ bower install MutationObserver-shim

CDN

<script src="//cdn.jsdelivr.net/npm/mutationobserver-shim/dist/mutationobserver.min.js"></script>

Polyfill differences from standard interface

MutationObserver

  • Implemented using a recursive setTimeout (every ~30 ms) rather than using a setImmediate polyfill; so calls will be made less frequently and likely with more data than the standard MutationObserver. In addition, it can miss changes that occur and then are lost in the interval window.
  • Setting an observed elements html using innerHTML will call childList observer listeners with several mutations with only 1 addedNode or removed node per mutation. With the standard you would have 1 call with multiple nodes in addedNodes and removedNodes node lists.
  • With childList and subtree changes in node order (eg first element gets swapped with last) should fire a addedNode and removedNode mutation but the correct node may not always be identified.

MutationRecord

  • addedNodes and removedNodes are arrays instead of NodeLists
  • oldValue is always called with attribute changes
  • nextSibling and previousSibling correctfullness is questionable (hard to know if the order of appended items). I'd suggest not relying on them anyway (my tests are extremely permissive with these attributes)

Supported MutationObserverInit properties

Currently supports the following MutationObserverInit properties:

  • childList: Set to truthy if mutations to target's immediate children are to be observed.
  • subtree: Set to truthy to do deep scans on a target's children.
  • attributes: Set to truthy if mutations to target's children are to be observed. As explained in #4, the style attribute may not be matched in ie<8.
  • attributeFilter: Set to an array of attribute local names (without namespace) if not all attribute mutations need to be observed.
  • attributeOldValue: doesn't do anything attributes are always called with old value
  • characterData: currently follows Mozilla's implementation in that it will only watch textNodes values and not, like in webkit, where setting .innerHTML will add a characterData mutation.

Performance

By default, the polyfill will check observed nodes about 25 times per second (30 ms interval) for mutations. Try running these jsperf.com tests and the JSLitmus tests in the test suite for usage performance tests. It may be worthwile to adapt MutationObserver._period based on UA or heuristics (todo).

From my tests observing any size element without subtree enabled is relatively cheap. Although I've optimized the subtree check to the best of my abilities it can be costly on large trees. You can draw your own conclusions based on the JSLitmus and jsperf tests noting that you can expect the mo to do its check 28+ times a second (by default).

Although supported, I'd recommend against watching attributes on the subtree on large structures, as the check is complex and expensive on terrible hardware like my phone :(

The included minified file has been tuned for performance.

Compatibility

I've tested and verified compatibility in the following browsers + these Sauce browsers

  • Internet Explorer 8 (emulated), 9, 10 in win7 and win8
  • Firefox 4, 21, 24, 26 in OSX, win7 and win8
  • Opera 11.8, 12.16 in win7
  • "Internet" on Android HTC One V
  • Blackberry 6.0.16

Try running the test suite and see some simple example usage:

See http://dev.opera.com/articles/view/mutation-observers-tutorial/ for some sample usage.

mutationobserver.js's People

Contributors

megawac avatar mnaoumov avatar arfaian avatar delros avatar gheoan avatar gyeates avatar kilian avatar meirionhughes avatar rafeca avatar siawo avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.