Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Input total number of executions</h1>

<input class="input__total-executions" placeholder="Total number of executions"/>

<button class="button__await">Execute slow method</button>

<button class="button__all">Execute efficient method</button>
              
            
!

CSS

              
                body {
  display: flex;
  background-color: #A17891;
}

button {
  padding: 8px;
  background-color: transparent;
  color: white;
  border: 1px solid white;
  margin-left: 8px;
}

button:hover {
  cursor: pointer;
}

h1 {
  color: white;
  font-family: Arial;
  font-size: 1rem;
  margin-right: 8px;
}
              
            
!

JS

              
                // Open your console to see the execution of the methods in real-time

/**
 * Returns the execution time of an asynchronous operation
 * @param {*} task 
 * @returns {Number} total
 */
const getExecutionTime = async (task) => {
  let startTime;
  let endTime;
  
  startTime = performance.now();
  await task();
  endTime = performance.now();
  
  return (endTime - startTime);
}

const getUserData = async () => {
  // Get a random dog as our user's avatar
  const res = await fetch('https://dog.ceo/api/breeds/image/random')
  const { message } = await res.json()
	
	// Get our user's general data
	const user = await fetch('https://randomuser.me/api/')
	const { results } = await user.json()
}

const getUserDataFast = async () => {
  const [res, user] = await Promise.all([
    fetch('https://dog.ceo/api/breeds/image/random'), 
    fetch('https://randomuser.me/api/')
  ])
  const [{ message }, { results }] = await Promise.all([res.json(), user.json()])
}

/**
 * Run the above method X times and log the average run time
 */
const start = async (method, totalExec) => {
	let total = 0
  
  console.log(`Starting ${totalExec} runs`)

	for (let i = 0; i < totalExec; i++) {
		const time = await getExecutionTime(method)
		total = total + time
	}

	console.log(`Average execution time for ${totalExec} runs: ${total / totalExec}`)
}

////////

const totalExecInput = document.querySelector('.input__total-executions')
totalExecInput.value = 10

document.querySelector('.button__await').onclick = () => { start(getUserData, totalExecInput.value) }

document.querySelector('.button__all').onclick = () => { start(getUserDataFast, totalExecInput.value) }
              
            
!
999px

Console