question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hello there!

Do you have a recommendation for a fast h function? (framework-agnostic) I found a tweet from developit for a short one.

function h(el, props, ...kids) {
  el = document.createElement(el)
  for (let i in props) i in el
    ? el[i] = props[i]
    : el.setAttribute(i,props[i])
  el.append(...kids)
  return el
}

But after running some tests, I think this could be more performant. Sadly I do not have the knowledge where to look at. Thank you for the help! Pete

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Grafikartcommented, Mar 15, 2020

@PeteSolid I got an issue with this :

const items = [1, 2, 3, 4, 5]
html`
<ul>
  <li>
    <span>Hello</span>
    ${items.map(i => html`<span>${i}</span>`)}
  </li>
</ul>`

// li will received children like this ["<span>Hello</span>", Array[5]]

I first expected the <li> h() function to receive a children list with 6 items, but It will receive 2 children, one with the first inline <span> and one array with the 5 dynamic <span>{item}</span>.

I did a small code sandbox to show the problem : https://codesandbox.io/s/busy-sun-f4db5

@developit Is it an expacted behaviour ?

2reactions
Grafikartcommented, Mar 14, 2020

This function doesn’t handle kids that contains array close to textNode (or other Node). It won’t handle event listener too. Here is my current approach. I don’t know if it’s fast but it covers all the edge case I encountered at the moment (maybe it helps)

/**
 * @param {string} tagName
 * @param {object} attributes
 * @param {...HTMLElement|string} children
 * @return HTMLElement
 */
export function createElement (tagName, attributes = {}, ...children) {
  // Create the element
  const e = document.createElement(tagName)

  // Set attributes and event listeners
  for (const k of Object.keys(attributes || {})) {
    if (k.startsWith('on')) {
      e.addEventListener(k.substr(2).toLowerCase(), attributes[k])
    } else {
      e.setAttribute(k, attributes[k])
    }
  }

  // Flatten children (we can receive an array of array)
  children = children.reduce(function (acc, child) {
    return Array.isArray(child) ? [...acc, ...child] : [...acc, child]
  }, [])

  // Append children to the element
  for (const child of children) {
    if (typeof child === 'string' || typeof child === 'number') {
      e.appendChild(document.createTextNode(child))
    } else if (child instanceof HTMLElement) {
      e.appendChild(child)
    } else {
      console.error("Cannot add this element", child)
    }
  }
  return e
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

H Function (Fox's H-Function) - Statistics How To
The H function (sometimes called Fox's H function) is a very generally defined special function, due to Charles Fox (1928). It can be...
Read more >
Effect of 48 h Fasting on Autonomic Function, Brain ... - PubMed
48 h fasting resulted in higher parasympathetic activity and decreased resting frontal brain activity, increased anger, and improved prefrontal-cortex-related ...
Read more >
A fast algorithm to compute the H∞-norm of a transfer function ...
A fast algorithm is presented to compute the H ∞ - norm of a transfer function matrix, based on the relation between the...
Read more >
Fast maths functions - The Bela Knowledge Base
Function Range Number ABS Max Err REL Max Err(%) RMS Err Time Rate sinf ‑3.14, 3.14 500000 0.00000000 0.00000000 0.00000000 262041 1.00 sinf_neon ‑3.14, 3.14...
Read more >
8 Health Benefits of Fasting, Backed by Science - Healthline
Fasting has been shown to have many health benefits, from increased weight loss to better brain function. Here are 8 health benefits of ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found