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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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.

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

              
                const makeToken = (maxLength) => {
  const length = Math.floor(Math.random() * maxLength) + 1
  var result = '';
  var characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
  var charactersLength = characters.length;
  for (let i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * characters.length));
  }
  return result;
}

const createArray = (length, func) => {
  const array = new Array(length)
  for (let i = 0; i < length; i++) {
    array[i] = func()
  }
  return array
}

const createMap = (array) => {
  const map = {}
  for (let i = 0; i < array.length; i++) {
    map[array[i]] = true
  }
  return map
}

const tokenCount = 100
const maxTokenLength = 10
const handles = createArray(tokenCount, () => makeToken(maxTokenLength))
const topics = createArray(tokenCount, () => makeToken(maxTokenLength))
const handleMap = createMap(handles)
const topicMap = createMap(topics)
const input = "@ and @ went to see @ in their # to look at her new #."
  .replace(/@/g, () => '@'+handles[Math.floor(Math.random() * handles.length)])
  .replace(/#/g, () => '#'+topics[Math.floor(Math.random() * topics.length)])

const reAhandle = new RegExp('@([a-zA-Z0-9]+)\\b', 'g')
const reAtopic = new RegExp('#([a-zA-Z0-9]+)\\b', 'g')

const funcA = (input, handles, topics) => {
  return input
    .replace(new RegExp('@([a-zA-Z0-9]+)\\b', 'g'), (match, handle) => handles.includes(handle.toLowerCase()) ? `<a href="/user/${handle}">@${handle}</a>` : handle)
    .replace(new RegExp('#([a-zA-Z0-9]+)\\b', 'g'), (match, topic) => topics.includes(topic.toLowerCase()) ? `<a href="/topic/${topic}">#${topic}</a>` : topic)
}

const funcA2 = (input, handles, topics) => {
  return input
    .replace(reAhandle, (match, handle) => handles.includes(handle.toLowerCase()) ? `<a href="/user/${handle}">@${handle}</a>` : handle)
    .replace(reAtopic, (match, topic) => topics.includes(topic.toLowerCase()) ? `<a href="/topic/${topic}">#${topic}</a>` : topic)
}

const funcC = (input, handles, topics) => {
  return input
    .replace(new RegExp('@([a-zA-Z0-9]+)\\b', 'g'), (match, handle) => handleMap[handle.toLowerCase()] ? `<a href="/user/${handle}">@${handle}</a>` : handle)
    .replace(new RegExp('#([a-zA-Z0-9]+)\\b', 'g'), (match, topic) => topicMap[topic.toLowerCase()] ? `<a href="/topic/${topic}">#${topic}</a>` : topic)
}

const funcC2 = (input, handles, topics) => {
  return input
    .replace(reAhandle, (match, handle) => handleMap[handle.toLowerCase()] ? `<a href="/user/${handle}">@${handle}</a>` : handle)
    .replace(reAtopic, (match, topic) => topicMap[topic.toLowerCase()] ? `<a href="/topic/${topic}">#${topic}</a>` : topic)
}

const funcB = (input, handles, topics) => {
  return input.replace(
    new RegExp('@(' + handles.join('|') + ')\\b', 'gmi'), `<a href="/user/$1">@$1</a>`
  ).replace(
    new RegExp('#(' + topics.join('|') + ')\\b', 'gmi'), `<a href="/topic/$1">#$1</a>`
  );
}

const reBhandle = new RegExp('@(' + handles.join('|') + ')\\b', 'gmi')
const reBtopic = new RegExp('#(' + topics.join('|') + ')\\b', 'gmi')
const funcB2 = (input, handles, topics) => {
  return input.replace(
    reBhandle, `<a href="/user/$1">@$1</a>`
  ).replace(
    reBtopic, `<a href="/topic/$1">#$1</a>`
  );
}

const run = (func, repeat) => {
  if (repeat === 1) {
    const start = performance.now()
    let length = 0
    let result = func(input, handles, topics)
    length += result.length //použít výsledek, ať se volání funkceneodoptimalizuje pryč
    return performance.now() - start    
  } else {
    const start = performance.now()
    let length = 0
    for (let i = 0; i < repeat; i++) {
      let result = func(input, handles, topics)
      length += result.length //použít výsledek, ať se volání funkceneodoptimalizuje pryč
    }
    return performance.now() - start    
  }
}

//ověření výstupu
const resultA = funcA(input, handles, topics)
const resultA2 = funcA(input, handles, topics)
const resultB = funcB(input, handles, topics)
const resultB2 = funcB(input, handles, topics)
const resultC = funcC(input, handles, topics)
const resultC2 = funcC(input, handles, topics)

console.log('Input', input)
console.log('Output funcA', resultA)
console.log('Output funcA2', resultA2)
console.log('Output funcB', resultB)
console.log('Output funcB2', resultB2)
console.log('Output funcC', resultC)
console.log('Output funcC2', resultC2)
console.log('Results are equals: ', resultA === resultB, resultB === resultC, resultA === resultC, resultA === resultA2, resultB === resultB2, resultC === resultC2)

//měření
const repeatCount = 100_000
console.log('start')
console.log('funcA', run(funcA, repeatCount))
console.log('funcB', run(funcB, repeatCount))
console.log('funcC', run(funcC, repeatCount))
console.log('funcA2', run(funcA2, repeatCount))
console.log('funcB2', run(funcB2, repeatCount))
console.log('funcC2', run(funcC2, repeatCount))

              
            
!
999px

Console