Any advantage of using "static" for private consts?

Just wondering if there are any benefits to using

private static const

instead

private const

for private constants? Does this change if you have only one instance of a class or several? I suspect there staticmight be a slight advantage in memory performance / performance if you have multiple instances of the class.

+3
source share
3 answers

As mmsmatt pointed out, they save some memory. This is usually not the best place to save memory. You better worry about memory leaks, efficient file formats, and the presentation of data in general.

, , . instance.ident Class.ident. :

package  {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.utils.*;
    public class Benchmark extends Sprite {
        private static const delta:int = 0;
        private const delta:int = 0;        
        private var output:TextField;
        public function Benchmark() {
            setTimeout(doBenchmark, 1000);
            this.makeOutput();
        }
        private function doBenchmark():void {
            var i:int, start:int, sum:int, inst:int, cls:int;

            start = getTimer();
            sum = 0;
            for (i = 0; i < 100000000; i++) sum += this.delta;
            out("instance:", inst = getTimer() - start);

            start = getTimer();
            sum = 0;
            for (i = 0; i < 100000000; i++) sum += Benchmark.delta;
            out("class:", cls = getTimer() - start);

            out("instance is", cls/inst, "times faster");
        }   
        private function out(...args):void {
            this.output.appendText(args.join(" ") + "\n");
        }
        private function makeOutput():void {
            this.addChild(this.output = new TextField());
            this.output.width = stage.stageWidth;
            this.output.height = stage.stageHeight;
            this.output.multiline = this.output.wordWrap = true;
            this.output.background = true;          
        }       
    }
}
+7

private static const .

private const .

, .

+6

.

, , , , .

I say this because if you have a component that is created only once at a time (for example, a tooltip popup) and you completely delete it from memory after, this means that you use unnecessary memory as static because it will never disappear with this static variable. If this is something, you will have multiple instances (e.g. particles or several windows), then yes, it is better to use static, because they will use the same variable.

0
source

Source: https://habr.com/ru/post/1757345/


All Articles