#!/usr/bin/env node
const process = require('process');
function measureArgPerformance(argName, task) {
const startTime = process.hrtime(); // High-resolution real time
task();
const endTime = process.hrtime(startTime);
const durationMilliseconds = endTime[0] * 1000 + endTime[1] / 1000000; // Convert to milliseconds
return durationMilliseconds;
}
function main() {
let argName = process.argv[2]; // Get the argument name from the command line
let task = process.argv[3]; // Get the task from the command line
if (!argName || !task) {
console.error('Usage: node script.js <argument_name> <task>');
process.exit(1);
}
try {
// Execute the task function
const duration = measureArgPerformance(argName, eval(`(${task})`));
console.log(`${argName} task took ${duration.toFixed(4)} milliseconds`);
} catch (error) {
console.error(`Error executing task: ${error.message}`);
process.exit(1);
}
}
main();
Add your comment