<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
var ment = angular.module('ent', []);
ment.controller('EntCtrl', function($compile,$scope) {
window.compileE = compileE;
function compileE(code) {
var scope = $scope.$new();
$compile(code)(scope);
scope.$destroy();
}
});
eMent = angular.element('<div><div ng-controller="EntCtrl"></div></div>');
angular.element(document.body).append(eMent);
angular.bootstrap(eMent[0], ['ent']);
window.compileE('<div ng-click="click()"></div>');
window.compileE('<div onclick="click()"></div>');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
ng | |
native |
Test name | Executions per second |
---|---|
ng | 40738.1 Ops/sec |
native | 38927.4 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark involves creating an AngularJS application with two test cases: one using Angular's built-in ng
directive and another using native HTML attributes (onclick
) for the same functionality.
Script Preparation Code
The script preparation code defines an AngularJS module named "ent" with a single controller named "EntCtrl". The controller has a method called compileE
that takes a JavaScript expression as input, compiles it using $compile
, and executes it on a newly created scope. This allows the test to measure the performance of compiling and executing JavaScript code.
The script also sets up an AngularJS element with an ng-controller directive pointing to the "EntCtrl" controller.
Html Preparation Code
The HTML preparation code includes a reference to the AngularJS library (version 1.5.3).
Test Cases
There are two test cases:
ng
directive to compile and execute JavaScript code.onclick
) for the same functionality.Comparison of Options
The two options being compared are:
ng
directive uses a proprietary compiler that takes care of parsing, compiling, and executing the JavaScript code.onclick
) rely on the browser's built-in engine to execute the JavaScript code.Pros and Cons
Library: AngularJS
AngularJS is a JavaScript framework that provides a set of features and tools for building web applications. The ng
directive used in this benchmark is part of AngularJS, which allows developers to create dynamic web pages with data-binding capabilities.
In this context, AngularJS is used to provide a standardized way of compiling and executing JavaScript code, making it easier to compare different approaches.
Special JS Feature/Syntax
There are no special JS features or syntax mentioned in the benchmark that require any specific knowledge or expertise. However, it's worth noting that AngularJS uses a proprietary compiler that may not be immediately familiar to developers who are not already familiar with the framework.
Other Alternatives
Other alternatives for compiling and executing JavaScript code include:
These alternatives may offer different trade-offs in terms of performance, complexity, and ease of use, depending on the specific requirements of the application.