A Weibo post by @老赵 "Is the code generated by eval really inefficient? http://t.cn/zWTUBEo Contains personal attacks, please don't enter if you don't like it."
triggered the recent heated discussion about eval During the discussion, the leader @Franky and Hui Da @otakustay also gave wonderful data analysis.
I happened to have done a similar test before, so I followed suit and joined in the fun, providing two sets of data for your reference.
Update 1: Thanks to @otakustay’s guidance, in order to eliminate the impact of the eval('') call itself on the results, a new set of data A3 and B3 has been added. And retest all old data.
Update 2: Thanks to Mo Da @貘吃荍香 for the powerful Paizhuan, added 1). Test data after eval coverage of A4, B4; A5, B5; 2). A6, B6 eval aliases; 3). A7,B7 eval.call.
Test environment:
a. Machine: Intel(R) Corei7-2720 2.2Ghz (4 cores 8 threads), memory 8Gb
b. OS: Windows 7 Enterprise SP1 64-bit
c. Browser:
b.1 Google Chrome 21.0.1180.79 m
b.2 Firefox 14.0.1
b.3 IE9.0.8112.16421
d. Test method
d .1 Each use case is tested 5 times, and the minimum time is taken.
d.2 Firebug or Chrome Console was not turned on during the test. Turning on these tools will double the time, making it difficult to get the results of this use case within the effective time
Use Case A1:
We inline Call empty eval("") in the function
!function () {
var a = 1,
b = 2,
c = true;
function func() {
var d = 2;
e = !c;
eval("");
}
for (var i = 0; i func(i, i 1, i 2);
}
}();
Use case A2:
Comment out eval("") in the inline function
!function() {
var a = 1,
b = 2,
c = true;
function func() {
var d = 2;
e = !c;
//eval("");
}
for (var i = 0; i func(i, i 1, i 2);
}
}();
Use case A3:
excludes eval("" ) The impact of the call itself, we call eval("") in the outer function
!function() {
var a = 1,
b = 2,
c = true;
function func() {
var d = 2;
e = !c;
}
for (var i = 0; i eval("");
func(i, i 1, i 2 ; >
Copy code
function eval(){}
for (var i = 0; i func(i, i 1, i 2);
}
}();
Use case A5:
It is also a function call, not eval and another empty function f
Copy code
The code is as follows:
function f(){}
for (var i = 0; i func(i, i 1, i 2);
}
}();
Use Case A6:
Assign eval to another variable f, and then call f
Copy the code
var f = eval;
for (var i = 0; i func(i, i 1, i 2);
}
}();
Use Case A7:
Use eval.call to call
Copy Code
The code is as follows:
!function() {
}
}();
A组测试结果:
A1 | A2 | A3 | A4 | A5 | A6 | A7 | A1 : A2 | A1 : A3 | A1 : A4 | A4 : A5 | |
Chrome | 1612ms | 8ms | 1244ms | 897ms | 7ms | 718ms | 680ms | 201.5 | 1.3 | 1.8 | 128.1 |
Firefox | 2468ms | 69ms | 732ms | 2928ms | 134ms | 5033ms | 4984ms | 35.8 | 3.4 | 0.8 | 21.9 |
IE | 1207ms | 23ms | 233ms | 1147ms | 37ms | 148ms | 224ms | 52.5 | 5.2 | 1.0 | 31.0 |
for (var i = 0; i !function() {
var a = 1,
b = 2,
c = true;
!function () {
var d = 2;
e = !c;
eval("");
}();
}();
}
用例B2:
for (var i = 0; i !function() {
var a = 1,
b = 2,
c = true;
!function () {
var d = 2;
e = !c;
//eval("");
}();
}();
}
用例B3:
for (var i = 0; i !function() {
var a = 1,
b = 2,
c = true;
!function () {
var d = 2;
e = !c;
}();
}();
eval("");
}
用例B4:
var eval = function(){}
for (var i = 0; i !function() {
var a = 1,
b = 2,
c = true;
!function () {
var d = 2;
e = !c;
eval("");
}();
}();
}
用例B5:
var f = function(){}
for (var i = 0; i !function() {
var a = 1,
b = 2,
c = true;
!function () {
var d = 2;
e = !c;
f("");
}();
}();
}
用例B6:
var f = eval;
for (var i = 0; i !function() {
var a = 1,
b = 2,
c = true;
!function () {
var d = 2;
e = !c;
f("");
}();
}();
}
用例B7:
for (var i = 0; i !function() {
var a = 1,
b = 2,
c = true;
!function () {
var d = 2;
e = !c;
eval.call(null, '');
}();
}();
}
B组测试结果:
B1 | B2 | B3 | B4 | B5 | B6 | B7 | B1 : B3 | B1 : B2 | B1 : B4 | B4 : B5 | |
Chrome | 1569ms | 134ms | 1093ms | 1022ms | 173ms | 830ms | 916ms | 11.7 | 1.4 | 1.5 | 5.9 |
Firefox | 5334ms | 1017ms | 5503ms | 5280ms | 1171ms | 6797ms | 6883ms | 5.2 | 1.0 | 1.0 | 4.5 |
IE | 3933ms | 560ms | 680ms | 4118ms | 583ms | 745ms | 854ms | 7.0 | 5.8 | 1.0 | 111.3 |
Conclusion ( is limited to CASE in the text):
1. Repeated calls to eval itself are very time-consuming, even empty eval("");
2. eval has an impact on the execution efficiency of inline functions, which varies depending on the specific environment and code;
3. We can see that no matter which browser, whether it is group A or group B, 2 and 5 have better speed. It shows that no matter how the eval of the inline function in the example is called (even if eval is overwritten by an empty function), it will still have a great impact on the operating efficiency. The inference is (black box inference, non-authoritative, probably just conjecture) that as long as eval is found in the inline function, even if the eval is an overridden empty function, all externally defined variables and other contents will be initialized in Scope Variables. to the current Scope. Similarly, eval will have a greater impact on the optimization function of the JS engine at runtime for inline functions and reduce execution efficiency.
4. To make a digression, although IE10 is not used, but IE9 is used, the performance of eval processing is very good. IE has always been criticized by developers, but its rapid growth is worthy of recognition. This example is a good proof.
A more detailed analysis of the reasons. The descriptions in the following articles are very detailed and will not be repeated again. Welcome:) Especially Mo Da...
@老赵’s 《Is the code generated by eval really inefficient? 》
@Franky’s "Popular Science on Eval"
@otakustay’s "A Brief Talk on the Influence of Eval"

eval的意思是“评估”,是python中的一个内置函数,用于执行一个字符串表达式,并返回表达式的计算结果;即变量赋值时,等号右边的表示是写成字符串的格式,返回值就是这个表达式的结果。语法“eval(表达式[, globals[, locals]])”。

win下php禁止eval的方法:1、下载“PHP_diseval_extension”;2、找到服务器当前使用的PHP;3、打开PHP配置文件 ;4、添加“extension=diseval.so”代码;5、重启服务即可。

Java开发:如何使用JMH进行性能测试和基准测试引言:在Java开发过程中,我们经常需要测试代码的性能和效率。为了准确地评估代码的性能,我们可以使用JMH(JavaMicrobenchmarkHarness)工具,它是专门为Java开发者设计的一款性能测试和基准测试的工具。本文将介绍如何使用JMH进行性能测试和基准测试,并提供一些具体的代码示例。一、什

PHP邮件对接类的性能测试和调优方法引言随着互联网的发展,电子邮件已成为人们日常沟通的重要方式之一。在开发网站或应用程序时,经常需要使用PHP来发送和接收电子邮件。为了提高邮件发送和接收的效率,我们可以对PHP邮件对接类进行性能测试和调优。本文将介绍如何进行这些测试,并提供一些代码示例。性能测试性能测试可以帮助我们了解邮件对接类的性能瓶颈,并找出优化的方向。

PHPUnit是PHP中非常流行的单元测试框架,它也可以用作性能测试。本文将介绍如何使用PHPUnit进行性能测试。首先,需要了解PHPUnit的一些基本概念。PHPUnit中的测试用例(TestCase)被定义为一个类,该类继承了PHPUnitFrameworkTestCase类。测试用例类中有一个或多个测试方法(testmethods),每个测试方法使

C#开发中如何处理关键性能指标和性能测试,需要具体代码示例在C#开发中,性能是一个非常重要的考虑因素。当我们开发一个项目时,无论是桌面应用程序、Web应用程序还是移动应用程序,我们都希望它能够运行得足够快,并且在使用过程中不会出现卡顿或延迟的情况。因此,我们需要关注和处理关键性能指标,并进行性能测试来确保应用的高性能和稳定性。处理关键性能指标处理关键性能指标

Java开发:如何进行性能测试和负载测试,需要具体代码示例引言:随着互联网和软件行业的快速发展,性能测试和负载测试在软件开发中变得越来越重要。通过对软件系统进行性能测试和负载测试,可以评估系统的可靠性、稳定性和吞吐量等关键性能指标。本文将介绍Java开发中如何进行性能测试和负载测试,并提供一些具体的代码示例。一、性能测试的基本概念和方法:1.1性能测试的定

Gin框架是一个基于Go语言的轻量级Web框架,它具有高效、快速和易于使用的特点,在很多领域都有广泛的应用。但是,在日常业务开发中,针对Gin框架的性能测试和优化技巧并不容易,本文就为大家详细介绍一下。一、Gin框架的性能测试压力测试工具在进行性能测试之前,首先需要准备好相应的测试工具,这里推荐两个常用的压力测试工具:ApacheBench和wrk。Apac


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version