performance - Javascript forEach loop order of operations and execution -


this such beginner question have concern how global object updated. i'll show i'm doing , talk concern.

(function() {     var obj = {};     var observer = new mutationobserver(function(mutations) {         mutations.foreach(function(mutation) {             //do whatever mutation             //update obj variable         });     });     observer.observe(target, config); })(); 

my concern when mutations coming in back rapidly, guaranteed first mutations finish process before proceeding next batch? think understand how closures work don't think that's need here. thinking passing each batch of mutations function, maybe creating queue , processing way...is better way ensure processed in order came in? creating kind of callback when each mutation process completed, moving on next? or foreach loop handle me?

also while processing these mutations, think i'm blocking thread since done synchronously, correct? should use iife around each mutation , use settimeout() make asynchronous?

(function() {     var obj = {};     function processmutation(mutation) {         //do whatever mutation         //update obj variable     }     var observer = new mutationobserver(function(mutations) {         mutations.foreach(function(mutation) {             //this isn't iife since foreach iife?             settimeout(function() {                 //pass separate function , release foreach function                 processmutation(mutation);             }, 0);             //this ^ should make async?         });     });     observer.observe(target, config); })(); 

doing way, each mutation added stack inside processmutation function, making processing of mutations asynchronous , allowing main thread continue listening other mutations? because processing might require long time do...

hope question isn't general...would advice.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -