Skip to content

Language Shootout

A user-supported site Fastest, Shortest, Simplest

Regex-Redux

JavaScript Node #1 — Regex-Redux

Match and replace a fixed set of regular expressions over DNA data, one pattern at a time.

Time 2,166.0 ms
CPU time 2,211.7 ms
Peak memory 1,004,336 KB
gz 411 bytes — comments removed, gzipped
Style ★★★☆☆
Implementation JavaScript — JavaScript (Node.js) 24.10.0
By sysop-
Submitted September 24, 2026

Style assessment

This is the canonical Benchmarks Game approach: it leans entirely on the JS regex engine via match/replace chains and the standard fs/console APIs, which is exactly right for this task and free of wasted complexity. It loses points for cryptic single-letter names (i, q, qj, m, ilen) and dated comma-chained var declarations spanning the pattern array; using const/let and names like input/patterns with a small helper for the count loop would raise naming and structure without changing the (already appropriate) algorithm.

// The Computer Language Benchmarks Game
// http://benchmarksgame.alioth.debian.org/
//
// regex-dna program contributed by Jesse Millikan
// Base on the Ruby version by jose fco. gonzalez
// fixed by Matthew Wilson
// ported to Node.js and sped up by Roman Pletnev
// converted from regex-dna program
// fixed by Josh Goldfoot

var fs = require('fs'), i = fs.readFileSync('/dev/stdin', 'ascii'),
  ilen = i.length, clen, j,
  q = [/agggtaaa|tttaccct/ig, /[cgt]gggtaaa|tttaccc[acg]/ig,
    /a[act]ggtaaa|tttacc[agt]t/ig, /ag[act]gtaaa|tttac[agt]ct/ig,
    /agg[act]taaa|ttta[agt]cct/ig, /aggg[acg]aaa|ttt[cgt]ccct/ig,
    /agggt[cgt]aa|tt[acg]accct/ig, /agggta[cgt]a|t[acg]taccct/ig,
    /agggtaa[cgt]|[acg]ttaccct/ig],

i = i.replace(/^>.*\n|\n/mg, '');
clen = i.length;
for(j = 0; j<q.length; ++j) {
  var qj = q[j], m = i.match(qj);
  console.log(qj.source, m ? m.length : 0);
}

i = i.replace(/tHa[Nt]/g, "<4>")
     .replace(/aND|caN|Ha[DS]|WaS/g, "<3>")
     .replace(/a[NSt]|BY/g, "<2>")
     .replace(/<[^>]*>/g, "|")
     .replace(/\|[^|][^|]*\|/g, "-");

console.log(["", ilen, clen, i.length].join("\n"));

Back to Regex-Redux