{"id":32,"date":"2018-09-07T06:52:31","date_gmt":"2018-09-07T06:52:31","guid":{"rendered":"http:\/\/www.webagam.com\/?p=32"},"modified":"2020-12-11T23:19:34","modified_gmt":"2020-12-11T23:19:34","slug":"arrow-functions-constructor-bind-and-non-bind-functions-with-react-js","status":"publish","type":"post","link":"https:\/\/webagam.com\/pages\/2018\/09\/07\/arrow-functions-constructor-bind-and-non-bind-functions-with-react-js\/","title":{"rendered":"Arrow Functions, constructor bind and non bind functions with react.js"},"content":{"rendered":"<div id=\"wp_clap_f_32\" class=\"wp_clap_f\"><div class=\"wp_clap_f_container\"><div id=\"wp_clap_f_count_32\" class=\"wp_clap_f_count\">2<\/div> \r\n\t\t\t\t\t\t\t\t\t<div id=\"wp_clap_f_text_32\" class=\"wp_clap_f_text\">\r\n\t\t\t\t\t\t\t\t\t<span onclick=\"ClpJS.clap('https:\/\/webagam.com\/pages\/index.php','32','Clapping','Clapped','2');\">Clap\r\n\t\t\t\t\t\t\t\t\t<\/span>\r\n\t\t\t\t\t\t\t\t\t<\/div><\/div><\/div><p><strong>Arrow Function vs Constructor Bind vs Normal Function<\/strong><\/p>\n<p>Before I start, wanted to clarify that I understand there are lots of articles and blogs written on the difference between the inline arrow function, auto bind and explicit bind.<br \/>\nHere is mention of those,<\/p>\n<p><a href=\"https:\/\/flexport.engineering\/ending-the-debate-on-inline-functions-in-react-8c03fabd144\">https:\/\/flexport.engineering\/ending-the-debate-on-inline-functions-in-react-8c03fabd144<\/a><\/p>\n<p>An article which talks about react, inline arrow function and performance. <a href=\"https:\/\/cdb.reacttraining.com\/react-inline-functions-and-performance-bdff784f5578\">https:\/\/cdb.reacttraining.com\/react-inline-functions-and-performance-bdff784f5578<\/a><\/p>\n<p>Here is another good mention of what I try to highlight in my below post, i.e. about arrow function vs constructor bind vs normal function.<\/p>\n<p><a href=\"https:\/\/medium.com\/@charpeni\/arrow-functions-in-class-properties-might-not-be-as-great-as-we-think-3b3551c440b1\">https:\/\/medium.com\/@charpeni\/arrow-functions-in-class-properties-might-not-be-as-great-as-we-think-3b3551c440b1<\/a><\/p>\n<p>I prefer the following and suggest below to my team,<\/p>\n<p>1)\u00a0\u00a0\u00a0\u00a0\u00a0 Use functions\/method (no bind) and pass attributes to it rather than using \u201cthis\u201d. This is the fastest with no contention.<\/p>\n<p>2)\u00a0\u00a0\u00a0\u00a0\u00a0Try to bind in the constructor like \u201cthis. add = this.add.bind(this)\u201d . In most use cases &amp; this has worked better than arrow functions in major browsers.<\/p>\n<p>(Note <a href=\"https:\/\/cdb.reacttraining.com\/react-inline-functions-and-performance-bdff784f5578\">https:\/\/cdb.reacttraining.com\/react-inline-functions-and-performance-bdff784f5578<\/a> has a good mention of a use case, where binding in constructor always may impact performance in certain cases.)<\/p>\n<p>3)\u00a0\u00a0\u00a0\u00a0\u00a0 Use class level arrow function for eg: var add = ()=&gt;{\u2026.}<\/p>\n<p>or use @autobind<\/p>\n<p>Note: Above is not part of ES2016 and it may not be in ES2017. It may be in ES2018, but as we use Babel to transpile, it was ok to use this.<\/p>\n<p>4) Use the inline arrow function for components that needs it or in case the third option is not possible.<\/p>\n<p>Now the question is, \u201cwhether preferring another way than inline arrow function in render a premature performance optimization\u201d, here is my perspective on this quote. I also detail it in another post.<\/p>\n<p>As an architect, designer and coder in both front-end and back-end technologies (react.js, javascript and java\/jee), I see them as coding guidelines or standards to keep in mind for any developer to code than premature performance optimization. I advise my team to code with performance, security, logging &amp; auditing in mind.<\/p>\n<p>I see code optimization as first writing the code and then optimizing it. Adding these as coding guidelines will result in writing better code from the start. This way we only have to deal with less code to optimize later as guidelines take care of other cases.<\/p>\n<p>For example, in java, it is preferred to use \u201cStringBuilder\u201d instead of \u201cStringBuffer\u201d especially within a method or in a single-threaded application, although both do the same. I consider them as good coding practices to know, then calling it premature optimization.<\/p>\n<p>Here is a simple code to measure scenarios on the arrow, constructor bind, and normal functions.<\/p>\n<p><iframe style=\"width: 100%; height: 500px; border: 0; border-radius: 4px; overflow: hidden;\" src=\"https:\/\/codesandbox.io\/embed\/xpy97mk8oq?codemirror=1\" sandbox=\"allow-modals allow-forms allow-popups allow-scripts allow-same-origin\"><\/iframe><\/p>\n<p>You may try out to get a perspective of performance in different browsers.<\/p>\n<p>My experience was as below,<\/p>\n<p>In all browsers, normal functions without bind are executed much faster than the other two.<\/p>\n<p>In Chrome 68.0.3440.106 , constructor bind executed marginally faster than an arrow function.<\/p>\n<p>In Microsoft Edge 38.14393.2068.0, constructor bind executed significantly faster than arrow function.<\/p>\n<p>In Firefox 61.x 64 bit, arrow function executed faster than constructor bind function.<\/p>\n<p>The test was performed in Windows 10 64 bit OS.<\/p>\n<p>Chrome executed faster among the 3 browsers, but again it depends on the javascript engine and browser version.<\/p>\n<!-- WP-Clap --><div id=\"wp_clap_32\" class=\"wp_clap\"><!-- BEGIN WP-Clap --><h3 class=\"wp_clap_title\" >Clap<\/h3><div id=\"wp_clap_do_32\" class=\"wp_clap_do\"><a href=\"javascript:void(0);\" onclick=\"ClpJS.clap('https:\/\/webagam.com\/pages\/index.php','32','Clapping','Clapped','2');\"><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp_clap_img\" alt=\"Clap\" src=\"https:\/\/i0.wp.com\/www.webagam.com\/pages\/wp-content\/plugins\/wp-clap\/images\/clap_32x32.gif?w=800\" \/>Clap<\/a><\/div><div class=\"wp_clap_clappers\"><span class=\"wp_clap_single_clapper\"><span class=\"wp_clap_avatar\"><img loading=\"lazy\" decoding=\"async\" alt=\"Anonymous\" title=\"Anonymous\" src=\"http:\/\/www.gravatar.com\/avatar.php?gravatar_id=d41d8cd98f00b204e9800998ecf8427e&size=32&default=\" width=\"32\" height=\"32\"\/><\/span><span class=\"wp_clap_name\"><a href=\"\">Anonymous<\/a><span class=\"wp_clap_frequency\">(2)<\/span><\/span><\/span><div class=\"wp_clap_clear\"><\/div><\/div><!-- END WP-Clap --><\/div>","protected":false},"excerpt":{"rendered":"<div id=\"wp_clap_f_32\" class=\"wp_clap_f\"><div class=\"wp_clap_f_container\"><div id=\"wp_clap_f_count_32\" class=\"wp_clap_f_count\">2<\/div> \r\n\t\t\t\t\t\t\t\t\t<div id=\"wp_clap_f_text_32\" class=\"wp_clap_f_text\">\r\n\t\t\t\t\t\t\t\t\t<span onclick=\"ClpJS.clap('https:\/\/webagam.com\/pages\/index.php','32','Clapping','Clapped','2');\">Clap\r\n\t\t\t\t\t\t\t\t\t<\/span>\r\n\t\t\t\t\t\t\t\t\t<\/div><\/div><\/div><p>2 Clap Arrow Function vs Constructor Bind vs Normal Function Before I start, wanted to clarify that I understand there are lots of articles and blogs written on the difference between the inline arrow function, auto bind and explicit bind. Here is mention of those, https:\/\/flexport.engineering\/ending-the-debate-on-inline-functions-in-react-8c03fabd144 An article which talks about react, inline arrow function and performance. https:\/\/cdb.reacttraining.com\/react-inline-functions-and-performance-bdff784f5578 Here is&hellip; <span class=\"read-more-span\"><a href=\"https:\/\/webagam.com\/pages\/2018\/09\/07\/arrow-functions-constructor-bind-and-non-bind-functions-with-react-js\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Arrow Functions, constructor bind and non bind functions with react.js&#8221;<\/span><\/a><\/span><\/p>\n<!-- WP-Clap --><div id=\"wp_clap_32\" class=\"wp_clap\"><!-- BEGIN WP-Clap --><h3 class=\"wp_clap_title\" >Clap<\/h3><div id=\"wp_clap_do_32\" class=\"wp_clap_do\"><a href=\"javascript:void(0);\" onclick=\"ClpJS.clap('https:\/\/webagam.com\/pages\/index.php','32','Clapping','Clapped','2');\"><img decoding=\"async\" class=\"wp_clap_img\" alt=\"Clap\" src=\"http:\/\/www.webagam.com\/pages\/wp-content\/plugins\/wp-clap\/images\/clap_32x32.gif\" \/>Clap<\/a><\/div><div class=\"wp_clap_clappers\"><span class=\"wp_clap_single_clapper\"><span class=\"wp_clap_avatar\"><img loading=\"lazy\" decoding=\"async\" alt=\"Anonymous\" title=\"Anonymous\" src=\"http:\/\/www.gravatar.com\/avatar.php?gravatar_id=d41d8cd98f00b204e9800998ecf8427e&size=32&default=\" width=\"32\" height=\"32\"\/><\/span><span class=\"wp_clap_name\"><a href=\"\">Anonymous<\/a><span class=\"wp_clap_frequency\">(2)<\/span><\/span><\/span><div class=\"wp_clap_clear\"><\/div><\/div><!-- END WP-Clap --><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-32","post","type-post","status-publish","format-standard","hentry","category-javascript","category-react"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/padP6e-w","_links":{"self":[{"href":"https:\/\/webagam.com\/pages\/wp-json\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webagam.com\/pages\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webagam.com\/pages\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webagam.com\/pages\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webagam.com\/pages\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":9,"href":"https:\/\/webagam.com\/pages\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":304,"href":"https:\/\/webagam.com\/pages\/wp-json\/wp\/v2\/posts\/32\/revisions\/304"}],"wp:attachment":[{"href":"https:\/\/webagam.com\/pages\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webagam.com\/pages\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webagam.com\/pages\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}