Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
querySelector vs querySelector + cache
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_3_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3.1 Mobile/15E148 Safari/604.1
Browser:
Mobile Safari 18
Operating system:
iOS 18.3.2
Device Platform:
Mobile
Date tested:
one year ago
Test name
Executions per second
querySelector
3277568.2 Ops/sec
querySelector + cache
19231244.0 Ops/sec
HTML Preparation code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1 class="about-shop">Hello azula</h1> </body> </html>
Script Preparation code:
const cache = {}; function $(selector) { if (cache[selector] !== undefined) { console.count("cached"); return cache[selector]; } const cached = document.querySelector(selector); cache[selector] = cached; return cached };
Tests:
querySelector
const selector1 = document.querySelector(".about-shop"); const selector2 = document.querySelector(".about-shop"); const selector3 = document.querySelector(".about-shop");
querySelector + cache
const selector1 = $(".about-shop"); const selector2 = $(".about-shop"); const selector3 = $(".about-shop");