search
HomeWeb Front-endJS TutorialDetailed explanation of the implementation principles of high-performance JavaScript template engine_javascript skills

With the development of the web, front-end applications have become more and more complex, and back-end-based javascript (Node.js) has also begun to emerge. At this time, greater expectations have been placed on javascript, and at the same time, javascript MVC ideas have also begun to become popular. stand up. As the most important part in the separation of data and interface, the javascript template engine has attracted more and more attention from developers. In the past year, it has flourished in the open source community. It has been used in Twitter, Taobao, Sina Weibo, Tencent QQ Zone, and Tencent Weibo. They can be seen in large websites such as Bo.

This article will use the simplest example code to describe the principles of the existing javascript template engine, including the feature implementation principle of the new generation javascript template engine artTemplate. Welcome to discuss together.

artTemplate Introduction

artTemplate is a new generation of javascript template engine. It uses pre-compilation to achieve a qualitative leap in performance, and fully utilizes the characteristics of the javascript engine to achieve extremely excellent performance in both the front-end and back-end. In the rendering efficiency test under chrome, it is 25 and 32 times that of the well-known engines Mustache and micro tmpl respectively.

In addition to the performance advantages, the debugging capabilities are also worth mentioning. The template debugger can accurately locate the template statement that caused the rendering error, which solves the pain of being unable to debug during the process of writing templates, makes development more efficient, and avoids the entire application crashing due to a single template error.

artTemplate All this in 1.7kb(gzip)!

Basic principles of javascript template engine

Although each engine has different implementation methods from template syntax, syntax parsing, variable assignment, and string splicing, the key rendering principle is still the dynamic execution of javascript strings.

Regarding dynamic execution of javascript strings, this article uses a template code as an example:

This is a very simple template writing method, where "" is closeTag (logical statement closing tag). If openTag is followed by "=", the contents of the variable will be output.

HTML statements and variable output statements are output directly, and the parsed string is similar:

After syntax analysis is completed, the rendering method will usually be returned:

Rendering test:

In the render method above, the template variable assignment uses the with statement, and the string splicing uses the array push method to improve performance under IE6 and 7. The micro template engine tmpl developed by jQuery author john is a typical example of this method. For representatives, see: http://ejohn.org/blog/javascript-micro-templating/

It can be seen from the principle implementation that there are two problems to be solved in the traditional javascript template engine:

1. Performance: The template engine relies on the Function constructor when rendering. Function, like eval, setTimeout, and setInterval, provides a method of using text to access the JavaScript parsing engine, but the performance of executing JavaScript in this way is very low.

2. Debugging: Since it is a dynamic execution string, if an error is encountered, the debugger cannot capture the source of the error, making template BUG debugging extremely painful. In an engine without fault tolerance, local templates may even cause the entire application to crash due to data anomalies. As the number of templates increases, maintenance costs will increase dramatically.

artTemplate’s secret to efficiency

1. Pre-compilation

In the above template engine implementation principle, because template variables need to be assigned values, each rendering requires dynamic compilation of javascript strings to complete variable assignments. However, the compilation and assignment process of artTemplate is completed before rendering. This method is called "pre-compilation". The artTemplate template compiler will extract all template variables according to some simple rules and declare them in the head of the rendering function. This function is similar to:

This automatically generated function is just like a hand-written javascript function. With the same number of executions, both CPU and memory usage are significantly reduced, and the performance is close to the limit.

It is worth mentioning that many features of artTemplate are based on precompiled implementation, such as sandbox specifications and custom syntax.

2. Faster string addition method

Many people mistakenly believe that the array push method to concatenate strings is faster than =. You must know that this is only under the IE6-8 browser. Actual measurements show that using = in modern browsers is faster than the array push method, and in the v8 engine, using = is 4.7 times faster than array splicing. Therefore, artTemplate uses two different string splicing methods based on the characteristics of the javascript engine.

artTemplate debug mode principle

The front-end template engine is not like the back-end template engine. It is dynamically parsed, so the debugger cannot locate the error line number, and artTemplate uses a clever way to allow the template debugger to accurately locate the template statement that caused the rendering error, such as :

artTemplate supports two types of error capture, one is rendering error (Render Error) and compilation error (Syntax Error).

1. Rendering error

Rendering errors are generally caused by template data errors or variable errors. Only when an error is encountered during rendering, the debug mode will be entered to recompile the template, without affecting the normal template execution efficiency. The template compiler records the line number according to the template newline character, and the compiled function is similar:

When an error is encountered during execution, the line number corresponding to the exception template is immediately thrown. The template debugger then checks the statement corresponding to the template based on the line number and prints it to the console.

2. Compilation error

Compilation errors are generally template syntax errors, such as unqualified nesting, unknown syntax, etc. Since artTemplate does not perform complete lexical analysis, it cannot determine the location of the error source. It can only output the original text of the error message and source code for developers to judge.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),