Should I wrap an array each time (in regards of safety, memory and performance)
opened this issue · comments
I have one global variable that is accessed and processed in every way possible. I have been using Underscore and I want to switch to lazy.js whenever it is possible seeing this (so wow 💯 )
I have functions like these:
var Lazy = require('lazy.js')
// Get one
db.get = function get(query, pick, subListing = Lazy(global.listings)) {
console.log("===== get ===== ")
return _.pick(subListing.findWhere(query), pick) //'id', 'title', 'desc_', 'lat', 'lng', 'img', 'ara'
}
// Deactivate one
db.deactivate = function deactivate(id, subListing = Lazy(global.listings)) {
console.log("===== deactivate ===== ")
return subListing.some(elem => {
if (elem.id === id) {
elem.d = 1;
return true;
}
})
}
// Approve one
db.approve = function approve(id, subListing = global.listings) {
console.log("===== approve ===== ")
return _.some(subListing, elem => {
if (elem.id === id) {
elem.a = 1;
return true;
}
})
}
db.rejectDeep = function rejectDeep(key, value, subListing = global.listings) {
console.log("===== rejectDeep ===== ")
if (!value)
return subListing
var query = (item) => {
return giveOp.sanitize(item[key], {
allowedTags: [],
allowedAttributes: {}
}).toLowerCase().indexOf(value.toLowerCase()) > -1;
}
return _.reject(subListing, query)
}
// and the list is long
see some here: https://github.com/bacloud14/dz_listings/blob/main/helper_data.js
Which are simple logic and sometimes just renames of Underscore with Web jargon (so probably not the best implementation ever) anyway:
As you see subListing
is the main array that I act on each time. How should I create the Lazy instance ? on function call so each time, or keep a global Lazy instance like:
//in app.js
lazyListings = Lazy(global.listings)
Of course I assume it is 100% a wrapper that means state of array elements can mutate (no problem since Nodejs is single threaded and each function in my app is aware of this).
thanks a lot !