core


License

License

GroupId

GroupId

com.github.fomkin
ArtifactId

ArtifactId

levsha-core_sjs0.6_2.12
Last Version

Last Version

0.10.0
Release Date

Release Date

Type

Type

jar
Description

Description

core
core
Project URL

Project URL

https://github.com/fomkin/levsha
Project Organization

Project Organization

com.github.fomkin
Source Code Management

Source Code Management

https://github.com/fomkin/levsha

Download levsha-core_sjs0.6_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.fomkin/levsha-core_sjs0.6_2.12/ -->
<dependency>
    <groupId>com.github.fomkin</groupId>
    <artifactId>levsha-core_sjs0.6_2.12</artifactId>
    <version>0.10.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.fomkin/levsha-core_sjs0.6_2.12/
implementation 'com.github.fomkin:levsha-core_sjs0.6_2.12:0.10.0'
// https://jarcasting.com/artifacts/com.github.fomkin/levsha-core_sjs0.6_2.12/
implementation ("com.github.fomkin:levsha-core_sjs0.6_2.12:0.10.0")
'com.github.fomkin:levsha-core_sjs0.6_2.12:jar:0.10.0'
<dependency org="com.github.fomkin" name="levsha-core_sjs0.6_2.12" rev="0.10.0">
  <artifact name="levsha-core_sjs0.6_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.fomkin', module='levsha-core_sjs0.6_2.12', version='0.10.0')
)
libraryDependencies += "com.github.fomkin" % "levsha-core_sjs0.6_2.12" % "0.10.0"
[com.github.fomkin/levsha-core_sjs0.6_2.12 "0.10.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.11
org.scala-js : scalajs-library_2.12 jar 0.6.33

provided (1)

Group / Artifact Type Version
org.scala-lang : scala-compiler jar 2.12.11

test (3)

Group / Artifact Type Version
org.scala-js : scalajs-test-bridge_2.12 jar 0.6.33
com.lihaoyi : utest_2.12 jar 0.6.9
org.scalacheck : scalacheck_2.12 jar 1.14.0

Project Modules

There are no modules declared in this project.

Levsha

Levsha is a fast HTML template engine and Scala eDSL. Optimized templates works without additional memory allocation. Levsha supports changeset inference, which allows to use it as virtual-dom-like middleware.

Static rendering

You can use Levsha as a static HTML renderer.

// build.sbt
libraryDependencies += "com.github.fomkin" %% "levsha-core" % "0.9.0"
// In your code
import levsha.text.renderHtml
import levsha.dsl._
import html._

val features = Seq("Very fast", "Memory-effective")

val html = renderHtml {
  optimize {
    body(
      div(
        clazz := "title",
        backgroundColor @= "red", 
        "Hello, I'm Levsha!"
      ),
      ul(clazz := "list",
        features map { feature =>
          li(class := "item", feature)
        }
      )
    )
  }
}

println(html)
<body>
  <div style="background-color: red" class="title">Hello, I'm Levsha!</div>
  <ul class="list">
    <li class="item">Very fast</li>
    <li class="item">Memory-effective</li>
  </ul>
</body>

Benchmarks

Benchmarks show that Levsha is really fast. Unlike Twirl, Levsha's performance does not depend on template complexity.

Test Engine Ops/s
simpleHtml levsha 1336693,499
simpleHtml scalatags 533740,566
simpleHtml twirl 5950436,854
withConditionAndLoop levsha 1299646,768
withConditionAndLoop scalatags 531345,430
withConditionAndLoop twirl 239537,158
withVariables levsha 1140298,804
withVariables scalatags 483508,457
withVariables twirl 2146419,329

In your sbt shell.

bench/jmh:run .StaticRenderingComparision

As a virtual DOM

Levsha can be used as virtual-DOM-like middleware. Unlike other popular virtual DOM solutions, Levsha doesn't allocate additional memory for construction of a new virtual DOM copy. Also it does not allocate memory in changes inferring phase. Levsha's memory usage is constant.

// build.sbt
libraryDependencies += "com.github.fomkin" %%% "levsha-dom" % "0.9.0"
// In your code
import org.scalajs.dom._
import levsha.dom.render
import levsha.dom.event
import levsha.dsl._
import html._

case class Todo(id: String, text: String, done: Boolean)

def onSubmitClick() = {
  val input = document
    .getElementById("todo-input")
    .asInstanceOf[html.Input]
  val inputText = input.value
  // Reset input
  input.value = ""
  val newTodo = Todo(
    id = Random.alphanumeric.take(5).mkString,
    text = inputText,
    done = false
  )
  renderTodos(todos :+ newTodo)
}

def onTodoClick(todo: Todo) = {
  renderTodos(
    todos.updated(
      todos.indexOf(todo),
      todo.copy(done = !todo.done)
    )
  ) 
}

def renderTodos(todos: Seq[Todo]): Unit = render(document.body) {
  optimize {
    body(
      div(clazz := "title", "Todos"),
      ul(clazz := "list",
        todos map { todo =>
          li(
            todo match {
              case Todo(_, text, true) => strike(text)
              case Todo(_, text, false) => span(text)
            },
            event("click")(onTodoClick(todo))
          )
        }
      ),
      input(id := "todo-input", placeholder := "New ToDo"),
      button("Submit", event("click")(onSubmitClick()))
    )
  }
}

val todos = Seq(
  Todo("1", "Start use Levsha", done = false),
  Todo("2", "Lean back and have rest", done = false)
)

renderTodos(todos)

Memory allocation model explanation

As noted below Levsha does not make additional memory allocations if template optimized. It is possible because optimized template, in compile-time rewrites into calls of RenderContext methods (unlike other template engines which represent their templates as AST on-heap).

For example,

div(clazz := "content", 
  h1("Hello world"),
  p("Lorem ipsum dolor")
)

Will be rewritten to

Node { renderContext =>
  renderContext.openNode(XmlNs.html, "div")
  renderContext.setAttr(XmlNs.html, "class", "content")
  renderContext.openNode(XmlNs.html, "h1")
  renderContext.addTextNode("Hello world")
  renderContext.closeNode("h1")
  renderContext.openNode(XmlNs.html, "p")
  renderContext.addTextNode("Lorem ipsum dolor")
  renderContext.closeNode("p")
  renderContext.closeNode("div")
}

In turn, RenderContext (namely DiffRenderContext implementation) saves instructions in ByteBuffer to infer changes in the future.

Of course, Levsha optimizer does not cover all cases. When optimization can't be performed Levsha just applies current RenderContext to the unoptimized node.

ul(
  Seq(1, 2, 3, 4, 5, 6, 7).collect { 
    case x if x % 2 == 0 => li(x.toString)
  }
)

// ==>

Node { renderContext =>
  renderContext.openNode(XmlNs.html, "ul")
  Seq(1, 2, 3, 4, 5, 6, 7)
    .collect {
      case x if x % 2 == 0 => 
        Node { renderContext =>
          renderContext.openNode(XmlNs.html, "li")
          renderContext.addTextNode(x.toString)
          renderContext.closeNode("li")
        }
    }
    .foreach { childNode =>
      childNode.apply(renderContext)
    }
  renderContext.closeNode("ul")
}

When you write your Levsha templates, keep in your mind this list of optimizations:

  1. Nodes and attrs in branches of if expression will be moved to current RenderContext
  2. Same for cases of pattern matching
  3. xs.map(x => div(x)) will be rewritten into a while loop
  4. maybeX.map(x => div(x)) will be rewritten into an if expression
  5. void will be removed

The third item of this list shows us how to rewrite previous example so that optimization could be performed.

ul(
  Seq(1, 2, 3, 4, 5, 6, 7)
    .filter(x => x % 2 == 0)
    .map { x => li(x.toString) }
)

// ==>

Node { renderContext =>
  renderContext.openNode(XmlNs.html, "div")
  val iterator = Seq(1, 2, 3, 4, 5, 6, 7)
    .filter(x => x % 2 == 0)
    .iterator
  while (iterator.hasNext) {
    val x = iterator.next()
    renderContext.openNode(XmlNs.html, "li")
    renderContext.addTextNode(x.toString)
    renderContext.closeNode("li")
  }
  renderContext.closeNode("div")
}

Worthy to note

  1. The Tale of Cross-eyed Lefty from Tula and the Steel Flea

Versions

Version
0.10.0
0.9.1
0.9.0
0.8.0
0.7.2
0.7.1
0.7.0
0.6.1
0.6.0
0.5.0
0.4.2
0.4.1
0.4.0
0.3.0
0.2.0
0.1.1
0.1.0