javascript.lang.best-practice.zlib-async-loop.zlib-async-loop

Author
735
Download Count*
License
Creating and using a large number of zlib objects simultaneously can cause significant memory fragmentation. It is strongly recommended that the results of compression operations be cached or made synchronous to avoid duplication of effort.
Run Locally
Run in CI
Defintion
rules:
- id: zlib-async-loop
patterns:
- pattern-either:
- pattern-inside: |
for (...) {
...
}
- pattern-inside: |
while (...) {
...
}
- pattern-inside: |
do {
...
} while (...)
- pattern-inside: |
$SMTH.forEach(...)
- pattern-inside: |
$SMTH.map(...)
- pattern-inside: |
$SMTH.reduce(...)
- pattern-inside: |
$SMTH.reduceRight(...)
- pattern: zlib.$METHOD(...);
- metavariable-regex:
metavariable: $METHOD
regex: ^.+$(?<!Sync)
message: Creating and using a large number of zlib objects simultaneously can
cause significant memory fragmentation. It is strongly recommended that
the results of compression operations be cached or made synchronous to
avoid duplication of effort.
metadata:
references:
- https://nodejs.org/api/zlib.html#zlib_threadpool_usage_and_performance_considerations
category: best-practice
technology:
- javascript
license: Commons Clause License Condition v1.0[LGPL-2.1-only]
severity: WARNING
languages:
- javascript
- typescript
Examples
zlib-async-loop.js
const zlib = require('zlib');
const payload = Buffer.from('This is some data');
for (let i = 0; i < 30000; ++i) {
// ruleid: zlib-async-loop
zlib.deflate(payload, (err, buffer) => {});
}
[1,2,3].forEach((el) => {
// ruleid: zlib-async-loop
zlib.deflate(payload, (err, buffer) => {});
})
for (let i = 0; i < 30000; ++i) {
// ok: zlib-async-loop
zlib.deflateSync(payload);
}
// ok: zlib-async-loop
zlib.deflate(payload, (err, buffer) => {});
Short Link: https://sg.run/58yK