Replace `zlib.deflate` ⇒ `zlib.deflateSync`

JavaScript pattern

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


Apply with the Grit CLI
grit apply replace_zlib_deflate_to_zlib_deflateSync

Replace zlib.deflatezlib.deflateSync

BEFORE
const zlib = require('zlib');

const payload = Buffer.from('This is some data');

for (i = 0; i < 30000; ++i) {
    // BAD: zlib-async-loop
    zlib.deflate(payload, (err, buffer) => {});
}

[1,2,3].forEach((el) => {
    // BAD: zlib-async-loop
    zlib.deflate(payload, (err, buffer) => {});
})

for (i = 0; i < 30000; ++i) {
    // GOOD: zlib-async-loop
    zlib.deflateSync(payload);
}

while(i < 30000){
  // BAD: zlib-async-loop
  zlib.deflate(payload, (err, buffer) => {});
}

do {
  // BAD: zlib-async-loop
  zlib.deflate(payload, (err, buffer) => {});
} while(i < 30000)

// GOOD: zlib-async-loop
zlib.deflate(payload, (err, buffer) => {});
AFTER
const zlib = require('zlib');

const payload = Buffer.from('This is some data');

for (i = 0; i < 30000; ++i) {
    // BAD: zlib-async-loop
    zlib.deflateSync(payload, (err, buffer) => {});
}

[1,2,3].forEach((el) => {
    // BAD: zlib-async-loop
    zlib.deflateSync(payload, (err, buffer) => {});
})

for (i = 0; i < 30000; ++i) {
    // GOOD: zlib-async-loop
    zlib.deflateSync(payload);
}

while(i < 30000){
  // BAD: zlib-async-loop
  zlib.deflateSync(payload, (err, buffer) => {});
}

do {
  // BAD: zlib-async-loop
  zlib.deflateSync(payload, (err, buffer) => {});
} while(i < 30000)

// GOOD: zlib-async-loop
zlib.deflate(payload, (err, buffer) => {});