JavaScript memory management library. Works well with Backbone.js apps
Mem.js manages functions and objects. It allows save, get and cleanup outdated instances.
It helps reuse views / models / widgets / functions in your app. Mem.js allows to share stored objects and reuse cached instances.
Store object / function etc in Mem.js for next reusage. If object is function creates new function instance.
If object with same name and params already exist it will be reused (new instance will not be created).
All objects were set will not be removed next manage. Mem.set removes objects from next cleanup list.
Unique name
Function / object etc to store
Arguments to be transfered to Function constructor
var View = Backbone.View({});
// On set returns new stored function instance or object
var headerViewIns = Mem.set('headerView', View, {el: 'body'});
Returns stored objects from Mem.js
Unique name
var headerViewIns = Mem.get('headerView');
Removes stored objects from Mem.js, calls remove
and dispose
methods automatocally.
Unique name (not required)
Mem.set('headerView', View, {el: 'body'});
// Removes headerView
Mem.unset('headerView');
Without arguments unsets all stored objects.
Mem.set('headerView', View, {el: 'body'});
Mem.set('headerModel', Model, {name: 'Artyom'});
// Removes headerView and headerModel
Mem.unset();
Unsets stored functions and creates new one with same parameters.
Unique name (not required)
Mem.set('headerView', View, {el: 'body'});
Mem.reset('headerView');
Without arguments resets all stored objects.
Mem.set('headerView', View, {el: 'body'});
Mem.set('headerModel', Model, {name: 'Artyom'});
Mem.reset();
Manages stored objects, removes outdated.
Removes all outdated objects, change all not outdated object state to outdated.
Mem.set removes objects from next cleanup list.
Simple manage:
Mem.set('headerView', View, {el: 'body'});
// headerView will not be removed
Mem.manage();
// headerView will be removed
Mem.manage();
Manage with set:
var ins1 = Mem.set('headerView', View, {el: 'body'});
// headerView will not be removed
Mem.manage();
// new headerView instance will not be created because Mem.js stored same fn with same params.
// Old instsnse will be returned instead
ins1 = Mem.set('headerView', View, {el: 'body'});
// headerView will not be removed
Mem.manage();
// headerView will be removed
Mem.manage();
requirejs.config({
baseUrl: 'static/',
urlArgs: 'bust=' + Date.now(),
paths: {
underscore: 'assets/js/underscore',
mem: 'assets/js/mem'
},
shim: {
mem: {
deps: ['underscore'],
exports: 'Mem'
}
}
});
var Mem = require('mem');
<script src="assets/js/underscore.js" />
<script src="assets/js/mem.js" />
bower install mem.js