Performance measurement

Open this lesson in the live editor

Discover and understand

What evidence would show that performance measurement works across the relevant application boundary?

Performance.now observations are local intervals, not production guarantees. Repeated samples and a median reduce some noise but do not remove all experimental bias. This calculator reports the median of supplied samples without inventing measurements.

  1. Explain performance measurement using the actual supplied source.
  2. Predict and verify normal, changed and boundary outcomes.
  3. Distinguish local execution, simulation and real exported Node behavior.

Walkthrough

[
  {
    "line": 1,
    "source": "async function main(input) {",
    "explanation": "Follow the explicitly named operation and inspect its values."
  },
  {
    "line": 2,
    "source": "if(!input.length)return null;",
    "explanation": "Return the actual derived value to the caller; compare it with the stated expected result."
  },
  {
    "line": 3,
    "source": "const values=[...input].sort((a,b)=>a-b),i=Math.floor(values.length/2);",
    "explanation": "Bind the declared input or derived value in this function scope."
  },
  {
    "line": 4,
    "source": "return values.length%2?values[i]:(values[i-1]+values[i])/2;",
    "explanation": "Return the actual derived value to the caller; compare it with the stated expected result."
  },
  {
    "line": 5,
    "source": "}",
    "explanation": "Follow the explicitly named operation and inspect its values."
  }
]

Five runnable source examples

Normal contract

Predict the declared normal output, then execute the actual function.

async function main(input) {
if(!input.length)return null;
const values=[...input].sort((a,b)=>a-b),i=Math.floor(values.length/2);
return values.length%2?values[i]:(values[i-1]+values[i])/2;
}

Input

[
  3,
  1,
  2
]

Expected value or DOM contract

2
Changed input

Change only the input and compare the independently stated result.

async function main(input) {
if(!input.length)return null;
const values=[...input].sort((a,b)=>a-b),i=Math.floor(values.length/2);
return values.length%2?values[i]:(values[i-1]+values[i])/2;
}

Input

[
  1,
  3
]

Expected value or DOM contract

2
Boundary or rejected input

Inspect the boundary or invalid-data outcome without inventing a successful write.

async function main(input) {
if(!input.length)return null;
const values=[...input].sort((a,b)=>a-b),i=Math.floor(values.length/2);
return values.length%2?values[i]:(values[i-1]+values[i])/2;
}

Input

[]

Expected value or DOM contract

null
Debug the changed source

The length property is misspelled, changing boundary calculations.

async function main(input) {
if(!input.lenght)return null;
const values=[...input].sort((a,b)=>a-b),i=Math.floor(values.length/2);
return values.length%2?values[i]:(values[i-1]+values[i])/2;
}

Input

[
  3,
  1,
  2
]

Expected value or DOM contract

2
Executable independent regression checks

Three frozen expected results are compared with actual execution. Changing the implementation can fail the contract.

async function operation(input) {
if(!input.length)return null;
const values=[...input].sort((a,b)=>a-b),i=Math.floor(values.length/2);
return values.length%2?values[i]:(values[i-1]+values[i])/2;
}
async function main(){const cases=[{"input":[3,1,2],"expected":2},{"input":[1,3],"expected":2},{"input":[],"expected":null}];for(const c of cases){const actual=await operation(c.input);if(JSON.stringify(actual)!==JSON.stringify(c.expected))throw Error("Contract mismatch");}return {passed:cases.length};}

Input

null

Expected value or DOM contract

{
  "passed": 3
}

Guided build and three hints

[
  "Read the source and the active example\u2019s expected result.",
  "Write a prediction before running.",
  "Run the actual code or controlled DOM preview.",
  "Change one input, inspect the result and run the independent check.",
  "Repair the starter with the hints, then explain a second case."
]
[
  "Locate the first operation whose input or output differs from the expected contract.",
  "Compare the working reference with the changed operation; inspect types, boundaries and the returned result.",
  "Restore the intended operation, rerun the exact case, then add a different boundary input."
]

Verified answer / authored DOM contract

async function main(input) {
if(!input.length)return null;
const values=[...input].sort((a,b)=>a-b),i=Math.floor(values.length/2);
return values.length%2?values[i]:(values[i-1]+values[i])/2;
}

Troubleshooting, reflection and extension

The length property is misspelled, changing boundary calculations.
Which actual value, node or trace entry supports your explanation? What did this check not establish?
Add an independent realistic input and a failing case that causes no premature side effect. Explain the remaining deployment and accessibility review.

Limits

Actual JavaScript calculation on supplied inputs; any Node, HTTP, database or security policy shown here is an explicitly limited educational model, not a real server operation.

Prompt studio goals

[
  {
    "goal": "Explain this concept and source using the stated evidence boundary.",
    "prompt": "Explain this concept and source using the stated evidence boundary.\nLesson: Performance measurement\nObjective: Performance.now observations are local intervals, not production guarantees. Repeated samples and a median reduce some noise but do not remove all experimental bias. This calculator reports the median of supplied samples without inventing measurements.\nReference source:\nasync function main(input) {\nif(!input.length)return null;\nconst values=[...input].sort((a,b)=>a-b),i=Math.floor(values.length/2);\nreturn values.length%2?values[i]:(values[i-1]+values[i])/2;\n}\nExpected:\n2\nI will supply my changed source and actual errors. Do not request secrets. Distinguish models from real Node/HTTP. Require me to reproduce and verify your suggested changes."
  },
  {
    "goal": "Help me implement one feature; propose independent tests before code.",
    "prompt": "Help me implement one feature; propose independent tests before code.\nLesson: Performance measurement\nObjective: Performance.now observations are local intervals, not production guarantees. Repeated samples and a median reduce some noise but do not remove all experimental bias. This calculator reports the median of supplied samples without inventing measurements.\nReference source:\nasync function main(input) {\nif(!input.length)return null;\nconst values=[...input].sort((a,b)=>a-b),i=Math.floor(values.length/2);\nreturn values.length%2?values[i]:(values[i-1]+values[i])/2;\n}\nExpected:\n2\nI will supply my changed source and actual errors. Do not request secrets. Distinguish models from real Node/HTTP. Require me to reproduce and verify your suggested changes."
  },
  {
    "goal": "Review/debug my actual source against the expected result; do not claim execution.",
    "prompt": "Review/debug my actual source against the expected result; do not claim execution.\nLesson: Performance measurement\nObjective: Performance.now observations are local intervals, not production guarantees. Repeated samples and a median reduce some noise but do not remove all experimental bias. This calculator reports the median of supplied samples without inventing measurements.\nReference source:\nasync function main(input) {\nif(!input.length)return null;\nconst values=[...input].sort((a,b)=>a-b),i=Math.floor(values.length/2);\nreturn values.length%2?values[i]:(values[i-1]+values[i])/2;\n}\nExpected:\n2\nI will supply my changed source and actual errors. Do not request secrets. Distinguish models from real Node/HTTP. Require me to reproduce and verify your suggested changes."
  }
]