Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

2.2 KB
Raw Blame
import assert from 'node:assert/strict'
import { performance } from 'node:perf_hooks'
import { extractIconHref } from '../../src/main/repo-icon-source-href.ts'

// Regex comparator retained for the before/after measurement.
const html =
  /<link\b(?=[^>]*\brel=["'](?:icon|shortcut icon)["'])(?=[^>]*\bhref=["']([^"'?]+))[^>]*>/i
const object =
  /(?=[^}]*\brel\s*:\s*["'](?:icon|shortcut icon)["'])(?=[^}]*\bhref\s*:\s*["']([^"'?]+))[^}]*/i
const original = (source) => source.match(html)?.[1] ?? source.match(object)?.[1] ?? null

function measurePair(source) {
  original(source)
  extractIconHref(source)
  const beforeSamples = []
  const afterSamples = []
  for (let run = 0; run < 5; run++) {
    const measurements = [
      [original, beforeSamples],
      [extractIconHref, afterSamples]
    ]
    if (run % 2 === 1) {
      measurements.reverse()
    }
    for (const [fn, samples] of measurements) {
      const started = performance.now()
      fn(source)
      samples.push(performance.now() - started)
    }
  }
  return {
    beforeMs: beforeSamples.sort((a, b) => a - b)[2],
    afterMs: afterSamples.sort((a, b) => a - b)[2]
  }
}

for (const [source, expected] of [
  ['<link rel="icon" href="/favicon.ico?version=1">', '/favicon.ico'],
  ["<link href='/shortcut.svg' rel='shortcut icon'>", '/shortcut.svg'],
  ['{ rel: "icon", href: "/object.png?version=1" }', '/object.png'],
  ["{ href: '/shortcut-object.svg', rel: 'shortcut icon' }", '/shortcut-object.svg']
]) {
  assert.equal(original(source), expected)
  assert.equal(extractIconHref(source), expected)
}

const results = []
for (const size of [8192, 16384, 32768]) {
  for (const variant of ['no icon', 'rel without href', 'unterminated link starts']) {
    const source =
      variant === 'unterminated link starts'
        ? '<link '.repeat(Math.floor(size / 6))
        : 'a'.repeat(size) + (variant === 'rel without href' ? ' rel:"icon"' : '')
    assert.equal(extractIconHref(source), original(source))
    const { beforeMs, afterMs } = measurePair(source)
    results.push({
      variant,
      bytes: Buffer.byteLength(source),
      beforeMs,
      afterMs,
      speedup: beforeMs / afterMs
    })
  }
}
console.log(JSON.stringify({ node: process.version, platform: process.platform, results }, null, 2))