Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

Save Automatically?

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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <h2>100,000 Items</h2>
<div id="root"></div>

<div class="top-right">
  Read my <a href="https://chrissmith.xyz/super-fast-tree-view-in-javascript/" target="_blank">blog post</a>.
</div>
            
          
!

CSS

            
              body {
  font-family:sans-serif;
}

ul {
  list-style:none;
  padding-left:1.75em;
  li {
    border-bottom-left-radius:.75em;
    border-top-left-radius:.75em;
    margin:2px 0;
    position:relative;
    a {
      background-color:#eee;
      border-radius:50%;
      color:#000;
      display:inline-block;
      // font-weight:bold;
      height:1.5em;
      left:-1.5em;
      line-height:1.5em;
      position:absolute;
      text-align:center;
      text-decoration:none;
      width:1.5em;
      &.plus {
        background-color:#ded;
      }
      &.minus {
        background-color:#eee;
      }
      &:active {
        top:1px;
      }
    }
    span {
      display:inline-block;
      margin:.25em .5em;
    }
  }
}

.top-right {
  font-size:small;
  position:fixed;
  right:1em;
  top:1em;
}
            
          
!

JS

            
              console.clear();

const numItems = 100000;

/* data is in flat array format like this:
[
  {
    id: 'abc',
    name: 'ABCDE',
    parent: null
  },
  {
    id: 'def',
    name: 'DEFGH',
    parent: 'abc'
  }
]
*/

// display nunber with commas for thousands
document.querySelector('h2').textContent = numItems.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + ' Items';

function generateData() {
  console.time('generateData');
  let data = [];
  function randomString() {
    return Math.random().toString(36).substr(2, 5).toUpperCase();
  }
  function randomParent(dataLength) {
    // return null;
    const rnd = Math.floor(Math.random() * dataLength * 0.9);
    if (rnd < 1) return null;
    return data[rnd].id;
  }
  for (let i = 0; i < numItems; i++) {
    const dataLength = data.length;
    const item = {
      id: i.toString(),
      name: randomString(),
      parent: randomParent(dataLength)
    }
    data.push(item);
  }
  console.timeEnd('generateData');
  return data;
}

const data = generateData();

function orphans() {
  return data.filter(function(item) {
    return item.parent === null;
  });
}

function hasChildren(parentId) {
  return data.some(function(item) {
    return item.parent === parentId;
  });
}

function getChildren(parentId) {
  return data.filter(function(item) {
    return item.parent === parentId;
  });
}

function generateListItem(item) {
  console.time('generateListItem');
  const li = document.createElement('li');
  li.id = 'item-' + item.id;
  if (hasChildren(item.id)) {
    const a = document.createElement('a');
    a.href = '#';
    a.textContent = '+';
    a.classList.add('plus');
    a.addEventListener('click', expand);
    li.appendChild(a);
  }
  const span = document.createElement('span');
  span.textContent = item.name;
  li.appendChild(span);
  console.timeEnd('generateListItem');
  return li;
}

function expand(event) {
  console.time('expand');
  event.preventDefault();
  event.stopPropagation();
  const et = event.target,
        parent = et.parentElement,
        id = parent.id.replace('item-', ''),
        kids = getChildren(id),
        items = kids.map(generateListItem),
        ul = document.createElement('ul');
  items.forEach(function(li) {
    ul.appendChild(li);
  });
  parent.appendChild(ul);
  et.classList.remove('plus');
  et.classList.add('minus');
  et.textContent = '-';
  et.removeEventListener('click', expand);
  et.addEventListener('click', collapse);
  console.timeEnd('expand');
}

function collapse(event) {
  console.time('collapse');
  event.preventDefault();
  event.stopPropagation();
  const et = event.target,
        parent = et.parentElement,
        ul = parent.querySelector('ul');        
  parent.removeChild(ul);
  et.classList.remove('minus');
  et.classList.add('plus');
  et.textContent = '+';
  et.removeEventListener('click', collapse);
  et.addEventListener('click', expand);
  console.timeEnd('collapse');
}

function addOrphans() {
  const root = document.querySelector('#root'),
        orphansArray = orphans();
  if (orphansArray.length) {
    const items = orphansArray.map(generateListItem),
          ul = document.createElement('ul');
    items.forEach(function(li) {
      ul.appendChild(li);
    });
    root.appendChild(ul);
  }
}

addOrphans();
            
          
!
999px

Console