A utility to help reuse expensive objects inside performance critical code
Install as a script tag.
<script src="path/to/scratchpad.min.js"></script>
OR
use as AMD module
require([ 'scratchpad '], function( Scratchpad ){
// code...
});
OR
use with nodejs.
var Scratchpad = require('scratchpad');
Register your recyclable object.
// register a hypothetical vector class...
Scratchpad.register('vector', Vector);
Use a scratchpad in a performance critical function.
var myAlg = function( scratch, arg1, arg2, ... ){
var scratch = Scratchpad()
,vec = scratch.vector().set( 0, 0 ) // need to reinitialize... it's recycled!
;
// ...
return scratch.done( result );
};
// later...
while( awesome ){
myAlg( arg1, arg2, ... );
}
Or... better yet...
Wrap a performance critical function with Scratchpad to get a scratch instance and clean up automatically (don't need to call done.)
var myAlg = Scratchpad(function( scratch, arg1, arg2, ... ){
var vec = scratch.vector().set( 0, 0 ); // need to reinitialize... it's recycled!
//...
return result;
});
// later...
while( awesome ){
myAlg( arg1, arg2, ... );
}