wilsonpage / fastdom

Eliminates layout thrashing by batching DOM measurement and mutation tasks
6.85k stars 240 forks source link

fastdom.batch() #10

Closed wilsonpage closed 11 years ago

wilsonpage commented 11 years ago

A new API called batch would give a way for devs to create a a new batcher with a size limit. Let me explain with an example:

var batcher = new fastdom.Batch({ limit: 3 });

batcher.add(function(){ console.log('1'); });
batcher.add(function(){ console.log('2'); });
batcher.add(function(){ console.log('3'); });
batcher.add(function(){ console.log('4'); });

First frame

// '1'
// '2'
// '3'

Second Frame

// '4'

Is this something that should be automated inside the standard .read and .write API?

wilsonpage commented 11 years ago

We could measure time elapsed in a batch, and schedule remaining work for the next frame if we exceed an 'agreed' threshold (30/60 fps).

wilsonpage commented 11 years ago

By default we could make this threshold 16ms (60fps), but give users the option to increase this if they wish.

fastdom.set({ threshold: 32 });

This could be a concern that depending on the performance of the device work could end up being done on different frames. This could lead to FastDom introducing device specific bugs (not nice). This is something that should be considered.

wilsonpage commented 11 years ago

This is out of scope of FastDom. The new way of calling .defer() without any frames argument solves some of the problems mentioned here.