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

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                // Show Log consts
const showMethods = false;
const showNumbers = false;

// Data
const totalIterations = 100;
const length = 1e5;

// Calc Functions
const timeFn = (fn, numbers) => {
  const ti = Date.now();
  const output = fn(numbers);
  const tf = Date.now();
  return { time: tf - ti, output };
};

// MapFilter
const filterAuxFn = (n) => parseInt(n, 2) < 16; //to avoid creating a new function each iteration
const mapAuxFn = (n) => n.toString(2); //to avoid creating a new function each iteration
const mapFilterFn = (numbers) => numbers.filter(filterAuxFn).map(mapAuxFn);

// Reduce spread
const reduceSpreadAuxFn = (acc, n) => (n < 16 ? [...acc, n.toString(2)] : acc); //to avoid creating a new function each iteration
const reduceSpreadFn = (numbers) => numbers.reduce(reduceSpreadAuxFn, []);

// Reduce index
const reduceIndexAuxFn = (acc, n) => {
  //to avoid creating a new function each iteration
  if (n < 16) acc.push(n.toString(2));
  return acc;
};
const reduceIndexFn = (numbers) => numbers.reduce(reduceIndexAuxFn, []);

// ForEach
const forEachFn = (numbers) => {
  const output = [];
  for (const n in numbers) {
    if (n < 16) output.push(n.toString(2));
  }
  return output;
};

// Benchmark data
const methods = [
  {
    name: 'Map and Filter',
    fn: mapFilterFn,
    times: Array(totalIterations).fill(0),
    avg: 0,
  },
  {
    name: 'Reduce with Spread',
    fn: reduceSpreadFn,
    times: Array(totalIterations).fill(0),
    avg: 0,
  },
  {
    name: 'Reduce With Push',
    fn: reduceIndexFn,
    times: Array(totalIterations).fill(0),
    avg: 0,
  },
  {
    name: 'For Each',
    fn: forEachFn,
    times: Array(totalIterations).fill(0),
    avg: 0,
  },
];

// Numbers array creation
const numbers = Array(length)
  .fill()
  .map(() => Math.round(Math.random() * 32));
if (showNumbers) console.log(numbers.slice(10));

for (let count = 0; count < totalIterations; count++) {
  console.log('Iteration:', count);
  methods.forEach((m) => {
    const { time, output } = timeFn(m.fn, numbers);
    if (showMethods) {
      console.log(`${m.name}:`, time);
      if (showNumbers) console.log(output).slice(0, 10);
    }
    m.times[count] = time;
    m.avg += time / totalIterations;
  });
}

for (const item of methods) {
  console.log(item.name, Math.round(item.avg * 100) / 100, 'ms');
}

              
            
!
999px

Console