Skip to content

Instantly share code, notes, and snippets.

@mbklein
Created March 24, 2023 16:55
Show Gist options
  • Save mbklein/e3cfc36f93c0f8ce87b3390d648ab372 to your computer and use it in GitHub Desktop.
Save mbklein/e3cfc36f93c0f8ce87b3390d648ab372 to your computer and use it in GitHub Desktop.
sharp.metadata() vs probe-image-size benchmarks
#!/usr/bin/env node
const AWS = require('aws-sdk');
const Benchmark = require('benchmark');
const fs = require('fs');
const probe = require('probe-image-size');
const sharp = require('sharp');
function * urlGenerator () {
const ring = fs.readFileSync('./s3Urls.txt').toString().split(/\n/);
while (true) {
for (const id of ring) yield id;
}
}
function createReadStream (url) {
const s3 = new AWS.S3();
const { host, pathname } = new URL(url);
return s3.getObject({ Bucket: host, Key: pathname.replace(/^\//, '') }).createReadStream();
}
async function sharpMetadata (url) {
const meta = sharp({ page: 0, sequentialRead: true });
const result = await createReadStream(url).pipe(meta).metadata();
return result;
}
async function probeMetadata (url) {
const result = await probe(createReadStream(url));
return result;
}
let urls = urlGenerator();
const suite = new Benchmark.Suite();
suite
.add('sharp.metadata()', async function () {
await sharpMetadata(urls.next().value);
})
.add('probe()', async function () {
await probeMetadata(urls.next().value);
})
.on('cycle', function ({ target }) {
console.log(String(target));
urls = urlGenerator();
})
.on('complete', function () {
console.log('Fastest is', suite.filter('fastest').map('name'));
})
.run({ async: true, minSamples: 50 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment