
Security News
/Research
Popular node-ipc npm Package Infected with Credential Stealer
Socket detected malicious node-ipc versions with obfuscated stealer/backdoor behavior in a developing npm supply chain attack.
bin-packer
Advanced tools
Packs objects into bins of a specified capacity. Repacking algorithms have been moved to bin-repacker.
npm install bin-packer
obj: The array or object whose own enumerable property values are to be binned (property keys
are discarded). Modifies obj, though not its values, if it is an array, so pass in a shallow
copy if you want to preserve the original.sizeOf: A function from items in obj to their numerical sizes. Will be called multiple times
on each item by most algorithms. So if this would be an expensive operation, it is advisable to
supply a function that returns the memoized value.capacity: The maximum bin size.Each algorithm returns an object with the following keys:
bins: An array of arrays, each containing elements with total size less than or equal to
capacity.oversized: An array containing any elements which on their own have a size greater than
capacity.Opens a new bin whenever a value doesn't fit in the latest one.
Tries to fit new items sequentially in all opened bins before opening a new one.
Runs a sort, so the hardest to place items are placed first, then uses First Fit.
Sorts items largest to smallest like First Fit Decreasing and then places each one in the fullest bin into which it will fit. Best Fit Decreasing should generally be preferred to First Fit and First Fit Decreasing since the Best Fit algorithm uses binary search to find the target bin for each item rather than First Fit's linear search and is considerably faster.
Korf's Bin Completion algorithm for producing an optimal solution. Warning! Bin packing is an NP-hard problem. Time and resource consumption may be high.
Each bound function returns an object with the following keys:
bound: A lower bound on the number of bins required by an optimal solution.oversized: The number of oversized items.Simple to compute: the number of bins required if elements' sizes could be split across bins to fill each completely before opening a new one.
Martello and Toth's L2 lower bound on the number of bins required by an optimal solution. Combines the methodology of the L1 lower bound with the addition of a 'waste' component for each bin that can be shown not to be fully fillable.
Example JSON input:
[ { "size": 3.08, "label": "dolore" },
{ "size": 7.89, "label": "nulla" },
{ "size": 44.51, "label": "nostrud", "OVERSIZED": "Size is larger than capacity." },
{ "size": 6.62, "label": "proident" },
{ "size": 2.07, "label": "occaecat" },
{ "size": 0.79, "label": "consectetur" },
{ "size": 8.05, "label": "in" },
{ "size": 0.13, "label": "fugiat" },
{ "size": 2.88, "label": "eiusmod" },
{ "size": 5.56, "label": "nisi" }
]
Pack it into bins:
const binPacker = require('bin-packer')
//, data = JSON.parse(...)
, sizeOf = item => item['size']
, capacity = 10
, result = binPacker.bestFitDecreasing(data.slice(), sizeOf, capacity)
console.log("Bins: %O", result.bins)
console.log("Oversized: %O", result.oversized)
Results in an array of bins:
Bins: [
[
{ size: 7.89, label: 'nulla' },
{ size: 2.07, label: 'occaecat' }
],
[
{ size: 6.62, label: 'proident' },
{ size: 3.08, label: 'dolore' },
{ size: 0.13, label: 'fugiat' }
],
[
{ size: 5.56, label: 'nisi' },
{ size: 2.88, label: 'eiusmod' },
{ size: 0.79, label: 'consectetur' }
],
[ { size: 8.05, label: 'in' } ]
]
Oversized: [
{ size: 44.51, label: 'nostrud', OVERSIZED: 'Size is larger than capacity.' }
]
FAQs
bin-packing algorithms
We found that bin-packer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
/Research
Socket detected malicious node-ipc versions with obfuscated stealer/backdoor behavior in a developing npm supply chain attack.

Security News
TeamPCP and BreachForums are promoting a Shai-Hulud supply chain attack contest with a $1,000 prize for the biggest package compromise.

Security News
Packagist urges PHP projects to update Composer after a GitHub token format change exposed some GitHub Actions tokens in CI logs.