Published
Edited
Mar 1, 2022
1 fork
Importers
7 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
cellName = measurePerformance(
'Standard Measurement',
() => {
const timestamp = Date.now()
while (Date.now() < timestamp + 1000) {}
return 'Tada! 🎉'
}
)
Insert cell
Insert cell
md`**Result:** ${cellName}`
Insert cell
Insert cell
asyncResult = measurePerformanceAsync(
'Async Measurement',
async () => await new Promise(
resolve => setTimeout(x => resolve('Tada! 🐢 🎉'), 2000)
)
)
Insert cell
Insert cell
md`**Result:** ${asyncResult}`
Insert cell
Insert cell
performanceResults()
Insert cell
Insert cell
mutable performanceEntries = []
Insert cell
measurePerformance = (name, fn) => {
performance.mark(`obperf-start-${name}`)
const returnVal = fn()
performance.mark(`obperf-end-${name}`)
performance.measure(`obperf-${name}`, `obperf-start-${name}`, `obperf-end-${name}`)
const entries = performance.getEntriesByType('measure')
.filter(e => e.name.startsWith('obperf-'))
.map(e => ({ 'Name': e.name.replace('obperf-', ''), 'Duration (ms)': e.duration, 'Measured at (ms after page load)': e.startTime }))
.sort((a, b) => a - b)
mutable performanceEntries = entries
return returnVal
}
Insert cell
measurePerformanceAsync = async (name, fn) => {
performance.mark(`obperf-start-${name}`)
const returnVal = await fn()
performance.mark(`obperf-end-${name}`)
performance.measure(`obperf-${name}`, `obperf-start-${name}`, `obperf-end-${name}`)
const entries = performance.getEntriesByType('measure')
.filter(e => e.name.startsWith('obperf-'))
.map(e => ({ 'Name': e.name.replace('obperf-', ''), 'Duration (ms)': e.duration, 'Measured at (ms after page load)': e.startTime }))
.sort((a, b) => a - b)
mutable performanceEntries = entries
return returnVal
}
Insert cell
performanceResults = () => {
const clearEntriesButton = html`<button>Clear Entries</button>`
clearEntriesButton.addEventListener('click', e => {
mutable performanceEntries = []
performance.clearMarks()
})
return html`
${table(performanceEntries, { title: 'Performance Measurements' })}
${clearEntriesButton}
`
}
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more