Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sanitize
(version: 1)
Comparing performance of:
From preact sources vs Simple solution vs ChatGPT solution
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Tests:
From preact sources
const strings = [ 'dsafsdafasdf', '>asdf<span>sdfsdf', 'sdfdsf&sdf"sdfadsfs\'sadf\nsadfasdf', '', '123#<!-- sdfsdf -->', ] const ENCODED_ENTITIES = /["&<]/ function encodeEntities(str) { // Skip all work for strings with no entities needing encoding: if (str.length === 0 || ENCODED_ENTITIES.test(str) === false) return str let last = 0, i = 0, out = '', ch = '' // Seek forward in str until the next entity char: for (; i < str.length; i++) { switch (str.charCodeAt(i)) { case 34: ch = '"' break case 38: ch = '&' break case 60: ch = '<' break default: continue } // Append skipped/buffered characters and the encoded entity: if (i !== last) out = out + str.slice(last, i) out = out + ch // Start the next seek/buffer after the entity's offset: last = i + 1 } if (i !== last) out = out + str.slice(last, i) return out } for (let i = 0; i < 100; i++) { strings.map(encodeEntities) }
Simple solution
const strings = [ 'dsafsdafasdf', '>asdf<span>sdfsdf', 'sdfdsf&sdf"sdfadsfs\'sadf\nsadfasdf', '', '123#<!-- sdfsdf -->', ] const sanitizeMap = { '<': '<', '>': '>', '"': '"', } const sanitize = value => { if (value === undefined || value == null) { return null } return String(value).replace(/[<>"]/g, char => sanitizeMap[char]) } for (let i = 0; i < 100; i++) { strings.map(sanitize) }
ChatGPT solution
const strings = [ 'dsafsdafasdf', '>asdf<span>sdfsdf', 'sdfdsf&sdf"sdfadsfs\'sadf\nsadfasdf', '', '123#<!-- sdfsdf -->', ] function sanitizeChatGPT(value) { if (!value) return '' return value .replace(/&/g, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(/"/g, '"') .replace(/'/g, ''') } for (let i = 0; i < 100; i++) { strings.map(sanitizeChatGPT) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
From preact sources
Simple solution
ChatGPT solution
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:133.0) Gecko/20100101 Firefox/133.0
Browser/OS:
Firefox 133 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
From preact sources
26837.6 Ops/sec
Simple solution
21322.2 Ops/sec
ChatGPT solution
5815.6 Ops/sec
Related benchmarks:
cycle vs regex
cycle vs regex
cycle vs regex
11111 vs 222222
loop test regex
rest params vs arguments (rest params are fast)
Check boolean
regex v split parsez
RegEx vs For Loop4
Comments
Confirm delete:
Do you really want to delete benchmark?