H3XED

# Actionscript 3: Function Overhead

Nov 16, 2010   Programming   Nick Vogt   Comments
Please note that this post is over a year old and may contain outdated information.
Functions are useful tools that allow you to reuse code, reducing file size and keeping your application better organized. There is a trade-off however, and that is function overhead. An Actionscript 3 application will take a small hit in performance when moving a section of code out of the normal flow of the program and into a function.

This hit is very minimal in most cases, so you shouldn't avoid using functions by any means. For example, if you're only calling a function once per frame the overhead will be non-existent. Code organization is more important in these cases.

A case where function overhead could become a problem is when a function is called many times per frame. Perhaps you have a function that calculates a vector for projectiles, and you have thousands of projectiles being created per frame at times. In this case, it may be better to include the vector calculation in with the normal flow of code and avoid using a function, even if that vector calculation needs to be in a couple different areas.

Unfortunately the actual performance difference is difficult to measure. The following test took 572 milliseconds:

`var i:int = 0;var xPos:Number = 10;var yPos:Number = 15.2;var speed:Number = 10;var time1 = new Date();for(i = 0; i < 100000; i ++){   vector();}var time2 = new Date();trace(time2 - time1);function vector():void{   var radians = Math.atan2(158.9 - 24.2, -29.8 - 18.1);   var vX = Math.sin(radians) * speed;   var vY = Math.cos(radians) * speed;   xPos+= vX;   yPos+= vY;}`
While moving the function body code into the loop resulted in 536 milliseconds:

`var i:int = 0;var xPos:Number = 10;var yPos:Number = 15.2;var speed:Number = 10;var time1 = new Date();for(i = 0; i < 100000; i ++){   var radians = Math.atan2(158.9 - 24.2, -29.8 - 18.1);   var vX = Math.sin(radians) * speed;   var vY = Math.cos(radians) * speed;   xPos+= vX;   yPos+= vY;}var time2 = new Date();trace(time2 - time1);`
This is with 100,000 iterations of the code. Less iterations results in a smaller performance gap (naturally), but the results are not measurable much lower than that. If you're only calling a function a small number of times per frame, don't expect any noticeable performance hit from function overhead.

One more note related to functions. Because Actionscript 3 always passes primitive variable types by value (including Booleans, Integers, and Numbers), be careful when using a function that takes any of these as arguments. You will actually be initializing (temporarily for local space only) variables whenever you call the function, which could also impact performance.
{"ft_lang_label":"__label__en","ft_lang_prob":0.88729066,"math_prob":0.9825832,"size":4324,"snap":"2019-13-2019-22","text_gpt3_token_len":963,"char_repetition_ratio":0.12291667,"word_repetition_ratio":0.82287306,"special_character_ratio":0.24167438,"punctuation_ratio":0.14064363,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97505337,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-03-22T04:29:03Z\",\"WARC-Record-ID\":\"<urn:uuid:f73aa66f-f30c-4f0d-a4e0-eb109e1d0caf>\",\"Content-Length\":\"14439\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:6db910cc-eca9-45db-8629-fee270c1daa9>\",\"WARC-Concurrent-To\":\"<urn:uuid:5f74224b-217e-43a4-bde3-4363e494d49c>\",\"WARC-IP-Address\":\"104.28.25.93\",\"WARC-Target-URI\":\"https://www.h3xed.com/programming/actionscript-3-function-overhead\",\"WARC-Payload-Digest\":\"sha1:4QZ5JQRWGBWA6SFDIA4GABWRAYHORYHA\",\"WARC-Block-Digest\":\"sha1:MEWBV6Z3PO7YXMBBKFY2A4DQTHQIVXDU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-13/CC-MAIN-2019-13_segments_1552912202628.42_warc_CC-MAIN-20190322034516-20190322060516-00240.warc.gz\"}"}