Console.log (v27)

Revision 27 of this benchmark created on


Setup

class Logger {
    constructor(batchSize = 500, interval = 1000) {
        this.cache = [];
        this.batchSize = batchSize;
        this.interval = interval;

        this.startConsuming();
    }

    log(message) {
        this.cache.push({
            timestamp: new Date().toISOString(),
            message: message
        });
        if (this.cache.length >= this.batchSize) {
            this.consumeCache();
        }
    }

    startConsuming() {
        setInterval(() => {
            if (this.cache.length > 0) {
                this.consumeCache();
            }
        }, this.interval);
    }

    consumeCache() {
        const batch = this.cache.splice(0, this.batchSize);

        this.processBatch(batch);
    }

    processBatch(batch) {

        console.log(`Processing batch of ${batch.length} records`);
        console.table(batch);
    }
}

// Usage example
const logger = new Logger(500, 1000);

const customLog = function(log) {
	logger.log(log)
}

Test runner

Ready to run.

Testing in
TestOps/sec
An empty function
customLog('Logging stuff');
ready
Console.log
console.log('Logging stuff');
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.