He estado leyendo un poco sobre Eval(), que es una función que ejecuta javascript a partir de un string, pero que según es muy peligroso y consume muchos recursos, hasta que buscando alternativas, encontré dos muy interesantes y funcionan de igual manera, new Function() y constructor.constructor(). Quisiera saber si hay otras alternativas o si estas ultimas dos son optimas para ejecutar strings de javascript.
Para ello muestro un ejemplo con la API Highcharts.js, donde en las tres opciones se ejecuta perfectamente, pero el foros dicen que Eval() no debería usarse. ¿que opinan?
var grafica="Highcharts.chart(\"row3\", {chart: {type: \"bar\",height:1000},title: {text: \"HISTORICO DE PASES GENERADOS A LAS OFICINAS CORRESPONDIENTES\"},subtitle:{text:\"AÑO 2018\"},xAxis: {categories: [\"ADMINISTRACION\",\"ARCHIVO\",\"ASESORIA JURIDICA\",\"ATENCION AL CIUDADANO\",\"BIBLIOTECA\",\"CULTURA\",\"DEPORTE\",\"DESPACHO\",\"DIVISION DE COMUNINADES EDUCATIVAS\",\"DIVISION DE DOTACION LOGISTICA\",\"DIVISION DE GESTION INTERNA\",\"DIVISION DE JOVENES Y ADULTOS\",\"DIVISION DE NIÑOS, NIÑAS Y POBLACION CON NECESIDADES EDUCATIVAS ESPECIALES\",\"DIVISION DE SUPERVISION\",\"ESCUELA TECNICA\",\"FORMACION PERMANENTE\",\"GESTION HUMANA\",\"INFORMATICA\",\"PLANIFICACION\",\"PRENSA\",\"RECURSO PARA EL APRENDIZAJE\",\"REPRODUCCION\",\"SALA SITUACIONAL\",\"SEGURO SOCIAL\"],title: {text: null},labels:{style:{fontSize:\"12px\",color:\"#db1b32\"}}},yAxis: {min: 0,title: {text: \"NUMERO DE PASES\",align: \"high\"},labels: {overflow: \"justify\",style:{color:\"#db1b32\",fontSize:\"12px\"}}},tooltip: {valueSuffix: \" PASES\"},plotOptions: {bar: {dataLabels: {enabled: true}}},series: [{ name: \"PASES\",data: [66,17,10,36,8,9,12,20,15,33,28,22,26,46,63,25,13,52,18,41,47,89,5,61,74],dataLabels:{style:{fontSize:\"12px\"}}}],navigation: {menuStyle: {background: \"#E0E0E0\"}}});";
document.getElementById('eval').addEventListener('click',function(){eval(grafica);});
document.getElementById('newfunction').addEventListener('click',function(){new Function(grafica)();});
document.getElementById('constructor').addEventListener('click',function(){constructor.constructor(grafica)();});
<html lang="es">
<head>
<title></title>
<script src="https://code.highcharts.com/highcharts.src.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
</head>
<body>
<h2>¿cual es la forma mas rapida de ejecutar codigo String de Javascript?</h2>
<button id='eval'>Eval(stringCode);</button><br>
<button id='newfunction'>new Function(stringCode)();</button><br>
<button id='constructor'>constructor.constructor(stringCode)();</button>
<div id='row3'></div>
</body>