Skip to content

Conversation

@wooliet
Copy link
Contributor

@wooliet wooliet commented Dec 10, 2025

Select the first instance of title element. Otherwise, multiple title elements results in the title string repeating itself for each instance.

closes #780

Select the first instance of title element. Otherwise, multiple title
elements results in the title string repeating itself for each
instance.
@Kikobeats
Copy link
Member

Kikobeats commented Jan 24, 2026

I wrote a little bench and :first seems better than .first()

const cheerio = require('cheerio')
const { performance } = require('node:perf_hooks')

const html = `
<!doctype html>
<html>
  <head>
    <title>GBAF 2024 – Highlights - YouTube</title>
    <title>GBAF 2024 – Highlights - YouTube</title>
  </head>
  <body></body>
</html>
`

const ITERATIONS = 1_000_000

const $ = cheerio.load(html)

// warmup (important for JIT)
for (let i = 0; i < 10_000; i++) {
  $('title').first().text()
  $('title:first').text()
}

function bench(name, fn) {
  const start = performance.now()
  for (let i = 0; i < ITERATIONS; i++) fn()
  const end = performance.now()
  console.log(`${name}: ${(end - start).toFixed(2)} ms`)
}

bench('title.first()', () => {
  $('title').first().text()
})

bench('title:first selector', () => {
  $('title:first').text()
})

output

title.first(): 4869.02 ms
title:first selector: 3945.67 ms

@Kikobeats Kikobeats force-pushed the youtube-title-rule-use-first-selector branch from 12524d0 to a97b4a4 Compare January 24, 2026 10:43
@Kikobeats Kikobeats force-pushed the youtube-title-rule-use-first-selector branch from a97b4a4 to 3945571 Compare January 24, 2026 10:44
@Kikobeats Kikobeats merged commit 1783ba5 into microlinkhq:master Jan 24, 2026
20 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

YouTube title value is doubled

2 participants