Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Parse snippet 2
(version: 0)
Comparing performance of:
Original vs Improved vs With HTML
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var text = `Garena Free Fire 📱 Sobrevive al mejor Battle Royale diseñado para móviles. 🪂 50 jugadores caen en paracaídas en un mapa donde deberán enfrentarse para ser el último en pie. 👉 Disponible para Android, iOS y para PC y Mac con emulador. #FreeFire https://img.snibits.com/HYCVDi4`
Tests:
Original
const parse = (snippet) => { const isImage = (word) => { const pattern = /https:\/\/img\.snibits\.com\/\w/ return word.match(pattern) } const isUrl = (word) => { const pattern = /https:\/\/\w/ return word.match(pattern) } const isTag = (word) => { const pattern = /#\w/ return word.match(pattern) } const isSpace = (word) => { const pattern = /https:\/\/snibits\.com\/space\/\w/ return word.match(pattern) } const isYoutube = (word) => word.match(/https:\/\/youtu.be\/\w/) const getCleanedContent = (text) => { let imageCount = 0 let url = null let content = text .split('\n') .map((line) => line .split(/\s/) .filter((word) => { if (isImage(word)) { imageCount++ } return !isImage(word) || imageCount > 4 }) .join(' ') ) .join('\n') .trim() let words = content.split(/\s/) if (words.length > 0) { const lastWord = words[words.length - 1] if ((isUrl(lastWord) && imageCount === 0) || isSpace(lastWord)) { const lines = content.split('\n') let lastLine = lines[lines.length - 1].split(/\s/) url = lastLine.pop() lines[lines.length - 1] = lastLine.join(' ') content = lines.join('\n').trim() } } return [content, url] } const [cleanedContent, url] = getCleanedContent(snippet.text) const getName = (snippet) => { let lines = cleanedContent.split('\n') if (lines.length > 0) { const line = lines[0].trim() if (line.length <= 60 && !['.', ':'].includes(line[line.length - 1])) { return line } } return '' } const name = getName(snippet) const getTitle = (snippet) => { const limit = 60 if (name) { return name } if (cleanedContent) { let title = cleanedContent.split('\n')[0] if (title.length > limit) { title = `${title.substring(0, limit - 1)}…` } return title } return '' } const title = getTitle(snippet) const getImages = (snippet) => { return snippet.text .split(/\s/) .filter((word) => isImage(word)) .slice(0, 4) } const images = getImages(snippet) const getTags = (snippet) => { return snippet.text.split(/\s/).filter((word) => isTag(word)) } const tags = getTags(snippet) const getHtml = (snippet) => { const content = cleanedContent.replace(name, '').trim() const limit = 27 const parseWord = (word) => { if (isUrl(word)) { const url = word let displayUrl = word.replace('https://', '') if (displayUrl.length > limit) { displayUrl = `${displayUrl.substring(0, limit - 1)}…` } return `<a href="${url}" target="_blank" rel="nofollow" class="content-link">${displayUrl}</a>` } else if (isTag(word)) { const tag = word.replace('#', '') return `<a href="/tag/${tag}">#${tag}</a>` } return word } const parseLine = (line) => { const parsedLine = line.split(/\s/).map((word) => parseWord(word)) return parsedLine.join(' ') } const lines = content.split('\n') let parsedLines = lines.map((line) => parseLine(line)) return parsedLines.join('\n').trim() } const html = getHtml(snippet) const getCount = (snippet) => { const limit = 27 const content = cleanedContent.replace(name, '').trim() const lines = content.split('\n') const parsedContent = lines .map((line) => line .split(/\s/) .map((word) => { if (isUrl(word) || isImage(word)) { return 'x'.repeat(limit) } return word }) .join(' ') ) .join('\n') return [...name].length + [...parsedContent].length + images.length * limit + (url ? limit : 0) } const count = getCount(snippet) return { name, title, images, html, count, url, tags } } parse({text})
Improved
const parse = (snippet) => { const isImage = (word) => { return word.startsWith('https://img.snibits.com/') } const isUrl = (word) => { return word.startsWith('https://') } const isTag = (word) => { return word.startsWith('#') } const isSpace = (word) => { return word.startsWith('https://snibits.com/space/') } const isLineName = (line) => { const words = line.split(/\s/) const firstWord = words[0] const lastChar = line[line.length - 1] const isValidFirstWord = !isImage(firstWord) && !isUrl(firstWord) && !isTag(firstWord) && !isSpace(firstWord) if (isValidFirstWord && line.length <= 60 && !['.', ':'].includes(lastChar)) { return true } return false } const extract = (text) => { const lines = text.trim().split('\n') let name = null let url = null const images = [] const processedLines = [] const tags = [] lines.forEach((line, indexLine) => { const words = line.trim().split(/\s/) const resultLine = [] words.forEach((word, indexWord) => { const isLastWord = indexLine === lines.length - 1 && indexWord === words.length - 1 if (isImage(word) && images.length < 4) { images.push(word) } else if (isLastWord && (isSpace(word) || (isUrl(word) && images.length === 0))) { url = word } else { resultLine.push(word) } if (isTag(word)) { tags.push(word) } }) const processedLine = resultLine.join(' ').trim() if (processedLines.length === 0 && processedLine.length > 0 && isLineName(processedLine)) { name = processedLine } else { processedLines.push(processedLine) } }) const content = processedLines.join('\n').trim() return { images, url, name, tags, content, } } return extract(snippet.text) } parse({ text })
With HTML
var text = `Garena Free Fire 📱 Sobrevive al mejor Battle Royale diseñado para móviles. 🪂 50 jugadores caen en paracaídas en un mapa donde deberán enfrentarse para ser el último en pie. 👉 Disponible para Android, iOS y para PC y Mac con emulador. #FreeFire https://img.snibits.com/HYCVDi4` const parse = (snippet) => { const ULR_LIMIT = 27 const isImage = (word) => { return word.startsWith('https://img.snibits.com/') } const isUrl = (word) => { return word.startsWith('https://') } const isTag = (word) => { return word.startsWith('#') } const isSpace = (word) => { return word.startsWith('https://snibits.com/space/') } const isLineName = (line) => { const words = line.split(/\s/) const firstWord = words[0] const lastChar = line[line.length - 1] const isValidFirstWord = !isImage(firstWord) && !isUrl(firstWord) && !isTag(firstWord) && !isSpace(firstWord) if (isValidFirstWord && line.length <= 60 && !['.', ':'].includes(lastChar)) { return true } return false } const parseHtmlWord = (word) => { if (isUrl(word)) { const url = word let displayUrl = word.replace('https://', '') if (displayUrl.length > limit) { displayUrl = `${displayUrl.substring(0, limit - 1)}…` } return } else if (isTag(word)) { } return word } const extract = (text) => { const lines = text.trim().split('\n') let name = null let url = null const images = [] const processedLines = [] const processedLinesHtml = [] const tags = [] lines.forEach((line, indexLine) => { const words = line.trim().split(/\s/) const resultLine = [] const resultLineHtml = [] words.forEach((word, indexWord) => { const isLastWord = indexLine === lines.length - 1 && indexWord === words.length - 1 if (isImage(word) && images.length < 4) { images.push(word) } else if (isLastWord && (isSpace(word) || (isUrl(word) && images.length === 0))) { url = word } else { resultLine.push(word) } }) const processedLine = resultLine.join(' ').trim() if (processedLines.length === 0 && processedLine.length > 0 && isLineName(processedLine)) { name = processedLine } else { processedLines.push(processedLine) resultLine.forEach((word, index) => { // HTML and tag detection if (isUrl(word)) { let displayUrl = word.replace('https://', '') if (displayUrl.length > ULR_LIMIT) { displayUrl = `${displayUrl.substring(0, limit - 1)}…` } resultLineHtml.push( `<a href="${word}" target="_blank" rel="nofollow" class="content-link">${displayUrl}</a>` ) } else if (isTag(word)) { tags.push(word) const slug = word.replace('#', '') resultLineHtml.push(`<a href="/tag/${slug}">${word}</a>`) } else { resultLineHtml.push(word) } }) processedLinesHtml.push(resultLineHtml) } }) const content = processedLines.join('\n').trim() const html = processedLinesHtml.join('\n').trim() return { images, url, name, tags, content, html, } } return extract(snippet.text) } parse({ text })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Original
Improved
With HTML
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
It looks like you're sharing some code and benchmark results related to testing the performance of a code snippet. I'll do my best to provide a concise answer. The code snippet appears to be a JavaScript function named `extract` that processes text data, extracting various elements such as images, URLs, tags, and content. The function also generates HTML content from the processed text. The benchmark results show three test runs with different names ("Improved", "With HTML", and "Original"). The results are in JSON format, showing metrics like executions per second, device platform, operating system, browser, and raw UA string (likely a user agent string). To provide a helpful answer, I'd need more context or information about what you're trying to achieve with this code. Are you optimizing the performance of the `extract` function? If so, what specific aspects are you focusing on?
Related benchmarks:
replacing node text
Parse snippet
TextEncoder.encode() vs encodeURIComponent
JS string compare
Comments
Confirm delete:
Do you really want to delete benchmark?