Skip to content

A utility to help reuse expensive objects inside performance critical code

License

Notifications You must be signed in to change notification settings

wellcaffeinated/scratchpad.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

scratchpad.js

A utility to help reuse expensive objects inside performance critical code

Getting Started

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');

Usage

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, ... );
}

Performance Test

http://jsperf.com/scratchpad-js

About

A utility to help reuse expensive objects inside performance critical code

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published